Skip to content

Commit

Permalink
Allow empty files to be uploaded.
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Sneeringer authored and dhermes committed Aug 7, 2017
1 parent fdaa12a commit 0e4fa01
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions google/resumable_media/_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ def get_next_chunk(stream, chunk_size, total_bytes):
# We now **KNOW** the total number of bytes.
total_bytes = end_byte + 1
else:
if num_bytes_read == 0:
if num_bytes_read == 0 and total_bytes != 0:
raise ValueError(
u'Stream is already exhausted. There is no content remaining.')

Expand All @@ -817,7 +817,7 @@ def get_next_chunk(stream, chunk_size, total_bytes):
raise ValueError(msg)

content_range = get_content_range(start_byte, end_byte, total_bytes)
return start_byte, end_byte, payload, content_range
return start_byte, max([end_byte, 0]), payload, content_range


def get_content_range(start_byte, end_byte, total_bytes):
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test__upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,13 @@ def test_exhausted_known_size(self):
exc_info.match(
u'Stream is already exhausted. There is no content remaining.')

def test_exhausted_known_size_zero(self):
data = b''
stream = io.BytesIO(data)
stream.seek(len(data))
answer = _upload.get_next_chunk(stream, 1, len(data))
assert answer == (0, 0, b'', 'bytes */0')

def test_read_past_known_size(self):
data = b'more content than we expected'
stream = io.BytesIO(data)
Expand Down

0 comments on commit 0e4fa01

Please sign in to comment.