diff --git a/.rubocop.yml b/.rubocop.yml index 8945eca..581ee2a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -11,7 +11,7 @@ Metrics/BlockLength: - "spec/**/*" Metrics/ClassLength: - Max: 102 + Max: 105 Naming/FileName: Exclude: diff --git a/lib/carrierwave/storage/aws_file.rb b/lib/carrierwave/storage/aws_file.rb index 192bb3b..478e4c3 100644 --- a/lib/carrierwave/storage/aws_file.rb +++ b/lib/carrierwave/storage/aws_file.rb @@ -58,7 +58,14 @@ def store(new_file) if new_file.is_a?(self.class) new_file.move_to(path) else - file.upload_file(new_file.path, aws_options.write_options(new_file)) + if Aws::S3.const_defined?(:TransferManager) + options = aws_options.write_options(new_file).except(:body) + options[:multipart_threshold] = AWSOptions::MULTIPART_THRESHOLD + Aws::S3::TransferManager.new(client: connection.client).upload_file(new_file.path, bucket: bucket.name, + key: path, **options) + else + file.upload_file(new_file.path, aws_options.write_options(new_file)) + end end end diff --git a/spec/carrierwave/storage/aws_file_spec.rb b/spec/carrierwave/storage/aws_file_spec.rb index 680ed34..19582ed 100644 --- a/spec/carrierwave/storage/aws_file_spec.rb +++ b/spec/carrierwave/storage/aws_file_spec.rb @@ -141,9 +141,24 @@ CarrierWave::SanitizedFile.new('spec/fixtures/image.png') end - it 'uploads the file using with multipart support' do - expect(file).to(receive(:upload_file) - .with(new_file.path, an_instance_of(Hash))) + it 'uploads the file using TransferManager' do + transfer_manager = instance_double('Aws::S3::TransferManager') + client = instance_double('Aws::S3::Client') + + allow(bucket).to receive(:name).and_return('example-com') + allow(connection).to receive(:client).and_return(client) + allow(Aws::S3::TransferManager).to receive(:new).with(client: client).and_return(transfer_manager) + + expect(transfer_manager).to receive(:upload_file).with( + new_file.path, + bucket: 'example-com', + key: path, + acl: :'public-read', + content_type: new_file.content_type, + encryption_key: 'def', + multipart_threshold: CarrierWave::Storage::AWSOptions::MULTIPART_THRESHOLD + ) + aws_file.store(new_file) end end