Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions storage/google/cloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,10 @@ def download_to_filename(self, filename, client=None):
with open(filename, 'wb') as file_obj:
self.download_to_file(file_obj, client=client)

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

This comment was marked as spam.

This comment was marked as spam.

if updated is not None:
mtime = time.mktime(updated.timetuple())
os.utime(file_obj.name, (mtime, mtime))

def download_as_string(self, client=None):
"""Download the contents of this blob as a string.
Expand Down
28 changes: 21 additions & 7 deletions storage/tests/unit/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,7 @@ def test_download_to_file_default(self):
def test_download_to_file_with_chunk_size(self):
self._download_to_file_helper(use_chunks=True)

@mock.patch('google.auth.transport.requests.AuthorizedSession')
def test_download_to_filename(self, fake_session_factory):
def _download_to_filename_helper(self, fake_session_factory, updated=None):
import os
import time
from google.cloud._testing import _NamedTemporaryFile
Expand All @@ -558,8 +557,10 @@ def test_download_to_filename(self, fake_session_factory):
_credentials=_make_credentials(), spec=['_credentials'])
bucket = _Bucket(client)
media_link = 'http://example.com/media/'
properties = {'mediaLink': media_link,
'updated': '2014-12-06T13:13:50.690Z'}
properties = {'mediaLink': media_link}
if updated is not None:
properties['updated'] = updated

blob = self._make_one(blob_name, bucket=bucket, properties=properties)
# Modify the blob so there there will be 2 chunks of size 3.
blob._CHUNK_SIZE_MULTIPLE = 1
Expand All @@ -569,14 +570,27 @@ def test_download_to_filename(self, fake_session_factory):
blob.download_to_filename(temp.name)
with open(temp.name, 'rb') as file_obj:
wrote = file_obj.read()
mtime = os.path.getmtime(temp.name)
updated_time = time.mktime(blob.updated.timetuple())
if updated is None:
self.assertIsNone(blob.updated)
else:
mtime = os.path.getmtime(temp.name)
updated_time = time.mktime(blob.updated.timetuple())
self.assertEqual(mtime, updated_time)

self.assertEqual(wrote, b'abcdef')
self.assertEqual(mtime, updated_time)

self._check_session_mocks(client, fake_session_factory, media_link)

@mock.patch('google.auth.transport.requests.AuthorizedSession')
def test_download_to_filename(self, fake_session_factory):
updated = '2014-12-06T13:13:50.690Z'
self._download_to_filename_helper(
fake_session_factory, updated=updated)

@mock.patch('google.auth.transport.requests.AuthorizedSession')
def test_download_to_filename_wo_updated(self, fake_session_factory):
self._download_to_filename_helper(fake_session_factory)

@mock.patch('google.auth.transport.requests.AuthorizedSession')
def test_download_to_filename_w_key(self, fake_session_factory):
import os
Expand Down