Skip to content

Commit 328bea8

Browse files
committed
fix: Empty last chunk case in resumable upload that is not a stream
For an upload that inherits the MediaUpload class and is not a stream, if the size of the upload is exact multiples of the chunksize, the last chunk would be empty and the generated Content-Range header will result in "Bad Request". Details: "Failed to parse Content-Range header."
1 parent 4b1cfc7 commit 328bea8

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

googleapiclient/http.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,10 +1062,14 @@ def next_chunk(self, http=None, num_retries=0):
10621062
"Content-Length": str(chunk_end - self.resumable_progress + 1),
10631063
}
10641064

1065-
# An empty file results in chunk_end = -1 and size = 0
1066-
# sending "bytes 0--1/0" results in an invalid request
1067-
# Only add header "Content-Range" if chunk_end != -1
1068-
if chunk_end != -1:
1065+
if chunk_end - self.resumable_progress + 1 == 0 and chunk_end != -1:
1066+
# This is the last chunk, it's empty but the file is not empty.
1067+
# Send a Content-Range that ends this file.
1068+
headers["Content-Range"] = "bytes */%s" % (size,)
1069+
elif chunk_end != -1:
1070+
# An empty file results in chunk_end = -1 and size = 0
1071+
# sending "bytes 0--1/0" results in an invalid request
1072+
# Only add header "Content-Range" if chunk_end != -1
10691073
headers["Content-Range"] = "bytes %d-%d/%s" % (
10701074
self.resumable_progress,
10711075
chunk_end,

0 commit comments

Comments
 (0)