Skip to content

Commit

Permalink
Reload missing properties before download.
Browse files Browse the repository at this point in the history
Fixes #1555.
  • Loading branch information
tseaver committed Mar 2, 2016
1 parent 1443984 commit 05ee5d0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions gcloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ def download_to_file(self, file_obj, client=None):
:raises: :class:`gcloud.exceptions.NotFound`
"""
client = self._require_client(client)
if self.media_link is None: # not yet loaded
self.reload()

download_url = self.media_link

# Use apitools 'Download' facility.
Expand Down
26 changes: 26 additions & 0 deletions gcloud/storage/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,32 @@ def test_delete(self):
self.assertFalse(blob.exists())
self.assertEqual(bucket._deleted, [(BLOB_NAME, None)])

def test_download_to_file_wo_media_link(self):
from six.moves.http_client import OK
from six.moves.http_client import PARTIAL_CONTENT
from io import BytesIO
BLOB_NAME = 'blob-name'
MEDIA_LINK = 'http://example.com/media/'
chunk1_response = {'status': PARTIAL_CONTENT,
'content-range': 'bytes 0-2/6'}
chunk2_response = {'status': OK,
'content-range': 'bytes 3-5/6'}
connection = _Connection(
(chunk1_response, b'abc'),
(chunk2_response, b'def'),
)
# Only the 'reload' request hits on this side: the others are done
# through the 'http' object.
reload_response = {'status': OK, 'content-type': 'application/json'}
connection._responses = [(reload_response, {"mediaLink": MEDIA_LINK})]
client = _Client(connection)
bucket = _Bucket(client)
blob = self._makeOne(BLOB_NAME, bucket=bucket)
fh = BytesIO()
blob.download_to_file(fh)
self.assertEqual(fh.getvalue(), b'abcdef')
self.assertEqual(blob.media_link, MEDIA_LINK)

def _download_to_file_helper(self, chunk_size=None):
from six.moves.http_client import OK
from six.moves.http_client import PARTIAL_CONTENT
Expand Down

0 comments on commit 05ee5d0

Please sign in to comment.