Skip to content

Commit

Permalink
Removing using of Blob.connection in all Blob.download* methods.
Browse files Browse the repository at this point in the history
Allowing the default connection to be used as fallback or takes
an explicit connection argument.
  • Loading branch information
dhermes committed Apr 14, 2015
1 parent 8ebb280 commit 824ff20
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
29 changes: 22 additions & 7 deletions gcloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,20 @@ def delete(self):
"""
return self.bucket.delete_blob(self.name)

def download_to_file(self, file_obj):
def download_to_file(self, file_obj, connection=None):
"""Download the contents of this blob into a file-like object.
:type file_obj: file
:param file_obj: A file handle to which to write the blob's data.
:type connection: :class:`gcloud.storage.connection.Connection` or
``NoneType``
:param connection: Optional. The connection to use when sending
requests. If not provided, falls back to default.
:raises: :class:`gcloud.exceptions.NotFound`
"""

connection = _require_connection(connection)
download_url = self.media_link

# Use apitools 'Download' facility.
Expand All @@ -282,37 +287,47 @@ def download_to_file(self, file_obj):
headers['Range'] = 'bytes=0-%d' % (self.chunk_size - 1,)
request = http_wrapper.Request(download_url, 'GET', headers)

download.InitializeDownload(request, self.connection.http)
download.InitializeDownload(request, connection.http)

# Should we be passing callbacks through from caller? We can't
# pass them as None, because apitools wants to print to the console
# by default.
download.StreamInChunks(callback=lambda *args: None,
finish_callback=lambda *args: None)

def download_to_filename(self, filename):
def download_to_filename(self, filename, connection=None):
"""Download the contents of this blob into a named file.
:type filename: string
:param filename: A filename to be passed to ``open``.
:type connection: :class:`gcloud.storage.connection.Connection` or
``NoneType``
:param connection: Optional. The connection to use when sending
requests. If not provided, falls back to default.
:raises: :class:`gcloud.exceptions.NotFound`
"""
with open(filename, 'wb') as file_obj:
self.download_to_file(file_obj)
self.download_to_file(file_obj, connection=connection)

mtime = time.mktime(self.updated.timetuple())
os.utime(file_obj.name, (mtime, mtime))

def download_as_string(self):
def download_as_string(self, connection=None):
"""Download the contents of this blob as a string.
:type connection: :class:`gcloud.storage.connection.Connection` or
``NoneType``
:param connection: Optional. The connection to use when sending
requests. If not provided, falls back to default.
:rtype: bytes
:returns: The data stored in this blob.
:raises: :class:`gcloud.exceptions.NotFound`
"""
string_buffer = BytesIO()
self.download_to_file(string_buffer)
self.download_to_file(string_buffer, connection=connection)
return string_buffer.getvalue()

def upload_from_file(self, file_obj, rewind=False, size=None,
Expand Down
6 changes: 3 additions & 3 deletions gcloud/storage/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def _download_to_file_helper(self, chunk_size=None):
blob._CHUNK_SIZE_MULTIPLE = 1
blob.chunk_size = chunk_size
fh = BytesIO()
blob.download_to_file(fh)
blob.download_to_file(fh, connection=connection)
self.assertEqual(fh.getvalue(), b'abcdef')

def test_download_to_file_default(self):
Expand Down Expand Up @@ -358,7 +358,7 @@ def test_download_to_filename(self):
blob._CHUNK_SIZE_MULTIPLE = 1
blob.chunk_size = 3
with NamedTemporaryFile() as f:
blob.download_to_filename(f.name)
blob.download_to_filename(f.name, connection=connection)
f.flush()
with open(f.name, 'rb') as g:
wrote = g.read()
Expand All @@ -385,7 +385,7 @@ def test_download_as_string(self):
blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties)
blob._CHUNK_SIZE_MULTIPLE = 1
blob.chunk_size = 3
fetched = blob.download_as_string()
fetched = blob.download_as_string(connection=connection)
self.assertEqual(fetched, b'abcdef')

def _upload_from_file_simple_test_helper(self, properties=None,
Expand Down

0 comments on commit 824ff20

Please sign in to comment.