diff --git a/gcloud/storage/key.py b/gcloud/storage/key.py index baaf7cc8587d..0adab3c5bdd3 100644 --- a/gcloud/storage/key.py +++ b/gcloud/storage/key.py @@ -17,6 +17,8 @@ import copy import mimetypes import os +import time +import datetime from StringIO import StringIO import urllib @@ -254,6 +256,13 @@ def download_to_filename(self, filename): with open(filename, 'wb') as file_obj: self.download_to_file(file_obj) + mtime = time.mktime( + datetime.datetime.strptime( + self.properties['updated'], + '%Y-%m-%dT%H:%M:%S.%fz').timetuple() + ) + os.utime(file_obj.name, (mtime, mtime)) + # NOTE: Alias for boto-like API. get_contents_to_filename = download_to_filename diff --git a/gcloud/storage/test_key.py b/gcloud/storage/test_key.py index a9e648cd4b7e..466a529435ad 100644 --- a/gcloud/storage/test_key.py +++ b/gcloud/storage/test_key.py @@ -215,6 +215,9 @@ def test_download_to_file(self): def test_download_to_filename(self): import httplib + import os + import time + import datetime from tempfile import NamedTemporaryFile KEY = 'key' chunk1_response = {'status': httplib.PARTIAL_CONTENT, @@ -227,7 +230,8 @@ def test_download_to_filename(self): ) bucket = _Bucket(connection) MEDIA_LINK = 'http://example.com/media/' - properties = {'mediaLink': MEDIA_LINK} + properties = {'mediaLink': MEDIA_LINK, + 'updated': '2014-12-06T13:13:50.690Z'} key = self._makeOne(bucket, KEY, properties) key.CHUNK_SIZE = 3 with NamedTemporaryFile() as f: @@ -235,7 +239,14 @@ def test_download_to_filename(self): f.flush() with open(f.name) as g: wrote = g.read() + mtime = os.path.getmtime(f.name) + updatedTime = time.mktime( + datetime.datetime.strptime( + key.properties['updated'], + '%Y-%m-%dT%H:%M:%S.%fz').timetuple() + ) self.assertEqual(wrote, 'abcdef') + self.assertEqual(mtime, updatedTime) def test_download_as_string(self): import httplib