Sha256: b70cca9ec163ccdf995ea3be1da62d93dc2c8803748f9451133ebbfab420e54a

Contents?: true

Size: 861 Bytes

Versions: 2

Compression:

Stored size: 861 Bytes

Contents

module IOStreams
  module S3
    class Reader
      # Read from a AWS S3 file
      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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
iostreams-0.18.0 lib/io_streams/s3/reader.rb
iostreams-0.17.3 lib/io_streams/s3/reader.rb