Sha256: 16c607c48e58db24574252303a873ccf9e08e4a9c2bc058c68ea1a05ab8a7ea9

Contents?: true

Size: 1.49 KB

Versions: 39

Compression:

Stored size: 1.49 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.open(source, 'rb') { |file| yield(file) }
        else
          yield(source)
        end
      end

    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
aws-sdk-s3-1.48.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.47.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.46.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.45.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.44.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.43.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.42.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.41.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.40.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.39.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.38.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.37.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.36.1 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.36.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.35.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.34.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.33.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.32.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.31.0 lib/aws-sdk-s3/file_uploader.rb
aws-sdk-s3-1.30.1 lib/aws-sdk-s3/file_uploader.rb