Skip to content

Commit 1cc3fcf

Browse files
committed
Address feedback for FileUploader and MultipartFileUploader
1 parent 9efc77f commit 1cc3fcf

5 files changed

Lines changed: 19 additions & 25 deletions

File tree

gems/aws-sdk-s3/lib/aws-sdk-s3/customizations/object.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,11 +459,17 @@ def upload_stream(options = {}, &block)
459459
# @see Client#upload_part
460460
def upload_file(source, options = {})
461461
uploading_options = options.dup
462-
uploader = FileUploader.new(multipart_threshold: uploading_options.delete(:multipart_threshold), client: client)
462+
executor = DefaultExecutor.new(max_threads: uploading_options.delete(:thread_count))
463+
uploader = FileUploader.new(
464+
client: client,
465+
executor: executor,
466+
multipart_threshold: uploading_options.delete(:multipart_threshold)
467+
)
463468
response = Aws::Plugins::UserAgent.metric('RESOURCE_MODEL') do
464469
uploader.upload(source, uploading_options.merge(bucket: bucket_name, key: key))
465470
end
466471
yield response if block_given?
472+
executor.shutdown
467473
true
468474
end
469475
deprecated(:upload_file, use: 'Aws::S3::TransferManager#upload_file', version: 'next major version')
@@ -543,8 +549,8 @@ def download_file(destination, options = {})
543549
downloader = FileDownloader.new(client: client, executor: executor)
544550
Aws::Plugins::UserAgent.metric('RESOURCE_MODEL') do
545551
downloader.download(destination, options.merge(bucket: bucket_name, key: key))
546-
executor.shutdown
547552
end
553+
executor.shutdown
548554
true
549555
end
550556
deprecated(:download_file, use: 'Aws::S3::TransferManager#download_file', version: 'next major version')

gems/aws-sdk-s3/lib/aws-sdk-s3/file_uploader.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class FileUploader
1313
# @option options [Client] :client
1414
# @option options [Integer] :multipart_threshold (104857600)
1515
def initialize(options = {})
16-
@options = options
1716
@client = options[:client] || Client.new
17+
@executor = options[:executor]
1818
@multipart_threshold = options[:multipart_threshold] || DEFAULT_MULTIPART_THRESHOLD
1919
end
2020

@@ -36,11 +36,9 @@ def initialize(options = {})
3636
# @return [void]
3737
def upload(source, options = {})
3838
Aws::Plugins::UserAgent.metric('S3_TRANSFER') do
39-
if File.size(source) >= multipart_threshold
40-
MultipartFileUploader.new(@options).upload(source, options)
39+
if File.size(source) >= @multipart_threshold
40+
MultipartFileUploader.new(client: @client, executor: @executor).upload(source, options)
4141
else
42-
# remove multipart parameters not supported by put_object
43-
options.delete(:thread_count)
4442
put_object(source, options)
4543
end
4644
end

gems/aws-sdk-s3/lib/aws-sdk-s3/multipart_file_uploader.rb

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@ class MultipartFileUploader
2020
)
2121

2222
# @option options [Client] :client
23-
# @option options [Integer] :thread_count (DEFAULT_THREAD_COUNT)
2423
def initialize(options = {})
2524
@client = options[:client] || Client.new
26-
@thread_count = options[:thread_count] || DEFAULT_THREAD_COUNT
27-
@executor = options[:executor] || DefaultExecutor.new(max_threads: @thread_count)
28-
@options = options
25+
@executor = options[:executor]
2926
end
3027

3128
# @return [Client]
@@ -45,8 +42,6 @@ def upload(source, options = {})
4542
upload_id = initiate_upload(options)
4643
parts = upload_parts(upload_id, source, file_size, options)
4744
complete_upload(upload_id, parts, file_size, options)
48-
ensure
49-
@executor.shutdown if @executor.running? && @options[:executor].nil?
5045
end
5146

5247
private
@@ -115,19 +110,19 @@ def checksum_key?(key)
115110
CHECKSUM_KEYS.include?(key)
116111
end
117112

118-
def checksum_keys?(keys)
113+
def has_checksum_keys?(keys)
119114
keys.any? { |key| checksum_key?(key) }
120115
end
121116

122117
def create_opts(options)
123118
opts = { checksum_algorithm: Aws::Plugins::ChecksumAlgorithm::DEFAULT_CHECKSUM }
124-
opts[:checksum_type] = 'FULL_OBJECT' if checksum_keys?(options.keys)
119+
opts[:checksum_type] = 'FULL_OBJECT' if has_checksum_keys?(options.keys)
125120
CREATE_OPTIONS.each_with_object(opts) { |k, h| h[k] = options[k] if options.key?(k) }
126121
end
127122

128123
def complete_opts(options)
129124
opts = {}
130-
opts[:checksum_type] = 'FULL_OBJECT' if checksum_keys?(options.keys)
125+
opts[:checksum_type] = 'FULL_OBJECT' if has_checksum_keys?(options.keys)
131126
COMPLETE_OPTIONS.each_with_object(opts) { |k, h| h[k] = options[k] if options.key?(k) }
132127
end
133128

@@ -173,8 +168,8 @@ def upload_with_executor(pending, completed, options)
173168
errors
174169
end
175170

176-
def compute_default_part_size(source_size)
177-
[(source_size.to_f / MAX_PARTS).ceil, MIN_PART_SIZE].max.to_i
171+
def compute_default_part_size(file_size)
172+
[(file_size.to_f / MAX_PARTS).ceil, MIN_PART_SIZE].max.to_i
178173
end
179174

180175
def part_size(total_size, part_size, offset)

gems/aws-sdk-s3/spec/file_uploader_spec.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ module S3
8181

8282
subject.upload(ten_meg_file.path, params)
8383
end
84-
85-
it 'does not fail when given :thread_count' do
86-
expect(client).to receive(:put_object).with(params.merge(body: ten_meg_file))
87-
88-
subject.upload(ten_meg_file, params.merge(thread_count: 1))
89-
end
9084
end
9185
end
9286
end

gems/aws-sdk-s3/spec/multipart_file_uploader_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module Aws
77
module S3
88
describe MultipartFileUploader do
99
let(:client) { S3::Client.new(stub_responses: true) }
10-
let(:subject) { MultipartFileUploader.new(client: client) }
10+
let(:executor) { DefaultExecutor.new }
11+
let(:subject) { MultipartFileUploader.new(client: client, executor: executor) }
1112
let(:params) { { bucket: 'bucket', key: 'key' } }
1213

1314
describe '#initialize' do

0 commit comments

Comments
 (0)