Class: IOStreams::S3::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/io_streams/s3/reader.rb

Class Method Summary collapse

Class Method Details

.open(uri, region: nil, **args, &block) ⇒ Object

Read from a AWS S3 file

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/io_streams/s3/reader.rb', line 5

def self.open(uri, region: nil, **args, &block)
  raise(ArgumentError, 'file_name must be a URI string') unless uri.is_a?(String)

  IOStreams::S3.load_dependencies

  s3      = region.nil? ? Aws::S3::Resource.new : Aws::S3::Resource.new(region: region)
  options = IOStreams::S3.parse_uri(uri)
  object  = s3.bucket(options[:bucket]).object(options[:key])

  begin
    # Since S3 download only supports a push stream, write it to a tempfile first.
    temp_file = Tempfile.new('rocket_job')
    temp_file.binmode

    args[:response_target] = temp_file.to_path
    object.get(args)

    block.call(temp_file)
  ensure
    temp_file.delete if temp_file
  end
end