Skip to content

Commit 623ea25

Browse files
authored
feat(core): Allow chunk size zero for storage resumable upload (#13283)
1 parent 60ff30a commit 623ea25

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

google-apis-core/lib/google/apis/core/storage_upload.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def send_upload_command(client)
141141
logger.debug { sprintf('Sending upload command to %s', @upload_url) }
142142

143143
remaining_content_size = upload_io.size - @offset
144-
current_chunk_size = remaining_content_size < @upload_chunk_size ? remaining_content_size : @upload_chunk_size
144+
current_chunk_size = get_current_chunk_size remaining_content_size
145145

146146
request_header = header.dup
147147
request_header[CONTENT_RANGE_HEADER] = get_content_range_header current_chunk_size
@@ -181,6 +181,15 @@ def streamable?(upload_source)
181181
upload_source.is_a?(IO) || upload_source.is_a?(StringIO) || upload_source.is_a?(Tempfile)
182182
end
183183

184+
def get_current_chunk_size remaining_content_size
185+
# Disable chunking if the chunk size is set to zero.
186+
if @upload_chunk_size == 0
187+
remaining_content_size
188+
else
189+
remaining_content_size < @upload_chunk_size ? remaining_content_size : @upload_chunk_size
190+
end
191+
end
192+
184193
def get_content_range_header current_chunk_size
185194
if upload_io.size == 0
186195
numerator = "*"

google-apis-core/spec/google/apis/core/storage_upload_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@
129129
end
130130
end
131131

132+
context('with chunking disabled') do
133+
let(:file) { StringIO.new("Hello world")}
134+
include_examples 'should upload'
135+
136+
it 'should upload content in one request' do
137+
command.options.upload_chunk_size = 0
138+
command.execute(client)
139+
expect(a_request(:put, 'https://www.googleapis.com/zoo/animals')
140+
.with(body: "Hello world")).to have_been_made
141+
end
142+
end
143+
132144
context 'with retriable error on start' do
133145
let(:file) { StringIO.new "Hello world"}
134146
before(:example) do

0 commit comments

Comments
 (0)