Class: EXEL::S3::S3Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/exel/s3/s3_provider.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.remote?(uri) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/exel/s3/s3_provider.rb', line 37

def self.remote?(uri)
  uri =~ %r{s3://}
end

Instance Method Details

#download(uri) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/exel/s3/s3_provider.rb', line 15

def download(uri)
  filename = uri.partition('://').last
  obj = get_object(filename)
  file = Tempfile.new(filename, encoding: Encoding::ASCII_8BIT)
  obj.get(response_target: file)
  file.set_encoding(Encoding::UTF_8)
  file
rescue Aws::S3::Errors::NoSuchKey => e
  raise EXEL::Error::JobTermination, "Aws::S3::Errors::NoSuchKey: #{e.message}"
end

#get_object(filename) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/exel/s3/s3_provider.rb', line 26

def get_object(filename)
  s3 = Aws::S3::Resource.new(
    credentials: Aws::Credentials.new(
      EXEL.configuration.aws.access_key_id,
      EXEL.configuration.aws.secret_access_key
    ),
    region: 'us-east-1'
  )
  s3.bucket(EXEL.configuration.s3_bucket).object(filename)
end

#upload(file) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/exel/s3/s3_provider.rb', line 6

def upload(file)
  filename = get_filename(file)
  obj = get_object(filename)
  obj.upload_file(file)
  file.close

  "s3://#{filename}"
end