Sha256: 5bc8fcc2baa0b30e2c0fe840342acb70710e40ef57b539a9a5d3f39e4e7afd02

Contents?: true

Size: 1.51 KB

Versions: 1032

Compression:

Stored size: 1.51 KB

Contents

require 'pathname'

module Aws
  module S3
    # @api private
    class FileUploader

      FIFTEEN_MEGABYTES = 15 * 1024 * 1024

      # @option options [Client] :client
      # @option options [Integer] :multipart_threshold Files greater than
      #   `:multipart_threshold` bytes are uploaded using S3 multipart APIs.
      def initialize(options = {})
        @options = options
        @client = options[:client] || Client.new
        @multipart_threshold = options[:multipart_threshold] || FIFTEEN_MEGABYTES
      end

      # @return [Client]
      attr_reader :client

      # @return [Integer] Files larger than this in bytes are uploaded
      #   using a {MultipartFileUploader}.
      attr_reader :multipart_threshold

      # @param [String,Pathname,File,Tempfile] source
      # @option options [required,String] :bucket
      # @option options [required,String] :key
      # @return [void]
      def upload(source, options = {})
        if File.size(source) >= multipart_threshold
          MultipartFileUploader.new(@options).upload(source, options)
        else
          put_object(source, options)
        end
      end

      private

      def put_object(source, options)
        open_file(source) do |file|
          @client.put_object(options.merge(body: file))
        end
      end

      def open_file(source)
        if String === source || Pathname === source
          file = File.open(source, 'rb')
          yield(file)
          file.close
        else
          yield(source)
        end
      end

    end
  end
end

Version data entries

1,032 entries across 1,032 versions & 3 rubygems

Version Path
aws-sdk-resources-2.11.632 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.631 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.630 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.629 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.628 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.627 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.626 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.625 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.624 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.623 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.622 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.621 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.620 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.619 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.618 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.617 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.616 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.615 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.614 lib/aws-sdk-resources/services/s3/file_uploader.rb
aws-sdk-resources-2.11.613 lib/aws-sdk-resources/services/s3/file_uploader.rb