Sha256: 7ecd9d03cf6996f78a6124c6eb9c1a390c2043d62bd41e319d503038a9a5e110

Contents?: true

Size: 873 Bytes

Versions: 1

Compression:

Stored size: 873 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.
          IOStreams::Path.temp_file_name('iostreams_s3') do |file_name|
            args[:response_target] = file_name
            object.get(args)

            # Return a read stream
            IOStreams::File::Reader.open(file_name, &block)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
iostreams-0.19.0 lib/io_streams/s3/reader.rb