Skip to content

Commit 05ee5d0

Browse files
committed
Reload missing properties before download.
Fixes #1555.
1 parent 1443984 commit 05ee5d0

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

gcloud/storage/blob.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ def download_to_file(self, file_obj, client=None):
287287
:raises: :class:`gcloud.exceptions.NotFound`
288288
"""
289289
client = self._require_client(client)
290+
if self.media_link is None: # not yet loaded
291+
self.reload()
292+
290293
download_url = self.media_link
291294

292295
# Use apitools 'Download' facility.

gcloud/storage/test_blob.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,32 @@ def test_delete(self):
291291
self.assertFalse(blob.exists())
292292
self.assertEqual(bucket._deleted, [(BLOB_NAME, None)])
293293

294+
def test_download_to_file_wo_media_link(self):
295+
from six.moves.http_client import OK
296+
from six.moves.http_client import PARTIAL_CONTENT
297+
from io import BytesIO
298+
BLOB_NAME = 'blob-name'
299+
MEDIA_LINK = 'http://example.com/media/'
300+
chunk1_response = {'status': PARTIAL_CONTENT,
301+
'content-range': 'bytes 0-2/6'}
302+
chunk2_response = {'status': OK,
303+
'content-range': 'bytes 3-5/6'}
304+
connection = _Connection(
305+
(chunk1_response, b'abc'),
306+
(chunk2_response, b'def'),
307+
)
308+
# Only the 'reload' request hits on this side: the others are done
309+
# through the 'http' object.
310+
reload_response = {'status': OK, 'content-type': 'application/json'}
311+
connection._responses = [(reload_response, {"mediaLink": MEDIA_LINK})]
312+
client = _Client(connection)
313+
bucket = _Bucket(client)
314+
blob = self._makeOne(BLOB_NAME, bucket=bucket)
315+
fh = BytesIO()
316+
blob.download_to_file(fh)
317+
self.assertEqual(fh.getvalue(), b'abcdef')
318+
self.assertEqual(blob.media_link, MEDIA_LINK)
319+
294320
def _download_to_file_helper(self, chunk_size=None):
295321
from six.moves.http_client import OK
296322
from six.moves.http_client import PARTIAL_CONTENT

0 commit comments

Comments
 (0)