Skip to content

Commit d51f29f

Browse files
committed
Merge pull request #751 from dhermes/use-storage-http-responses
Using copy and upload responses in storage.
2 parents 4512eff + 189e5fe commit d51f29f

File tree

5 files changed

+24
-19
lines changed

5 files changed

+24
-19
lines changed

gcloud/storage/blob.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Create / interact with Google Cloud Storage blobs."""
1616

1717
import copy
18+
import json
1819
import mimetypes
1920
import os
2021
import time
@@ -353,10 +354,17 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
353354
# pass them as None, because apitools wants to print to the console
354355
# by default.
355356
if upload.strategy == transfer._RESUMABLE_UPLOAD:
356-
upload.StreamInChunks(callback=lambda *args: None,
357-
finish_callback=lambda *args: None)
357+
http_response = upload.StreamInChunks(
358+
callback=lambda *args: None,
359+
finish_callback=lambda *args: None)
358360
else:
359-
http_wrapper.MakeRequest(conn.http, request, retries=num_retries)
361+
http_response = http_wrapper.MakeRequest(conn.http, request,
362+
retries=num_retries)
363+
response_content = http_response.content
364+
if not isinstance(response_content,
365+
six.string_types): # pragma: NO COVER Python3
366+
response_content = response_content.decode('utf-8')
367+
self._properties = json.loads(response_content)
360368

361369
def upload_from_filename(self, filename, content_type=None):
362370
"""Upload this blob's contents from the content of a named file.

gcloud/storage/bucket.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ def copy_blob(self, blob, destination_bucket, new_name=None):
364364
new_name = blob.name
365365
new_blob = Blob(bucket=destination_bucket, name=new_name)
366366
api_path = blob.path + '/copyTo' + new_blob.path
367-
self.connection.api_request(method='POST', path=api_path)
367+
copy_result = self.connection.api_request(method='POST', path=api_path)
368+
new_blob._properties = copy_result
368369
return new_blob
369370

370371
def upload_file(self, filename, blob_name=None):

gcloud/storage/test_blob.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def _upload_from_file_simple_test_helper(self, properties=None,
348348
DATA = b'ABCDEF'
349349
response = {'status': OK}
350350
connection = _Connection(
351-
(response, b''),
351+
(response, b'{}'),
352352
)
353353
bucket = _Bucket(connection)
354354
blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties)
@@ -412,10 +412,11 @@ def test_upload_from_file_resumable(self):
412412
chunk1_response = {'status': http_wrapper.RESUME_INCOMPLETE,
413413
'range': 'bytes 0-4'}
414414
chunk2_response = {'status': OK}
415+
# Need valid JSON on last response, since resumable.
415416
connection = _Connection(
416417
(loc_response, b''),
417418
(chunk1_response, b''),
418-
(chunk2_response, b''),
419+
(chunk2_response, b'{}'),
419420
)
420421
bucket = _Bucket(connection)
421422
blob = self._makeOne(BLOB_NAME, bucket=bucket)
@@ -470,7 +471,7 @@ def test_upload_from_file_w_slash_in_name(self):
470471
'range': 'bytes 0-4'}
471472
chunk2_response = {'status': OK}
472473
connection = _Connection(
473-
(loc_response, ''),
474+
(loc_response, '{}'),
474475
(chunk1_response, ''),
475476
(chunk2_response, ''),
476477
)
@@ -512,7 +513,7 @@ def _upload_from_filename_test_helper(self, properties=None,
512513
'range': 'bytes 0-4'}
513514
chunk2_response = {'status': OK}
514515
connection = _Connection(
515-
(loc_response, ''),
516+
(loc_response, '{}'),
516517
(chunk1_response, ''),
517518
(chunk2_response, ''),
518519
)
@@ -576,7 +577,7 @@ def test_upload_from_string_w_bytes(self):
576577
'range': 'bytes 0-4'}
577578
chunk2_response = {'status': OK}
578579
connection = _Connection(
579-
(loc_response, ''),
580+
(loc_response, '{}'),
580581
(chunk1_response, ''),
581582
(chunk2_response, ''),
582583
)
@@ -614,7 +615,7 @@ def test_upload_from_string_w_text(self):
614615
'range': 'bytes 0-4'}
615616
chunk2_response = {'status': OK}
616617
connection = _Connection(
617-
(loc_response, ''),
618+
(loc_response, '{}'),
618619
(chunk1_response, ''),
619620
(chunk2_response, ''),
620621
)
5 MB
Binary file not shown.

regression/storage.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class TestStorageFiles(unittest2.TestCase):
8888
'path': 'regression/data/CloudPlatform_128px_Retina.png',
8989
},
9090
'big': {
91-
'path': 'regression/data/five-mb-file.zip',
91+
'path': 'regression/data/five-point-one-mb-file.zip',
9292
},
9393
'simple': {
9494
'path': 'regression/data/simple.txt',
@@ -119,21 +119,19 @@ def test_large_file_write_from_stream(self):
119119

120120
file_data = self.FILES['big']
121121
with open(file_data['path'], 'rb') as file_obj:
122-
self.bucket.upload_file_object(file_obj, blob_name=blob.name)
122+
blob.upload_from_file(file_obj)
123123
self.case_blobs_to_delete.append(blob)
124124

125-
blob._reload_properties() # force a reload
126125
self.assertEqual(blob.md5_hash, file_data['hash'])
127126

128127
def test_small_file_write_from_filename(self):
129-
blob = storage.Blob(bucket=self.bucket, name='LargeFile')
128+
blob = storage.Blob(bucket=self.bucket, name='SmallFile')
130129
self.assertEqual(blob._properties, {})
131130

132131
file_data = self.FILES['simple']
133132
blob.upload_from_filename(file_data['path'])
134133
self.case_blobs_to_delete.append(blob)
135134

136-
blob._reload_properties() # force a reload
137135
self.assertEqual(blob.md5_hash, file_data['hash'])
138136

139137
def test_write_metadata(self):
@@ -143,7 +141,6 @@ def test_write_metadata(self):
143141
# NOTE: This should not be necessary. We should be able to pass
144142
# it in to upload_file and also to upload_from_string.
145143
blob.content_type = 'image/png'
146-
blob._reload_properties() # force a reload
147144
self.assertEqual(blob.content_type, 'image/png')
148145

149146
def test_direct_write_and_read_into_file(self):
@@ -153,7 +150,7 @@ def test_direct_write_and_read_into_file(self):
153150
self.case_blobs_to_delete.append(blob)
154151

155152
same_blob = storage.Blob(bucket=self.bucket, name='MyBuffer')
156-
same_blob._reload_properties() # force a reload
153+
same_blob._reload_properties() # Initialize properties.
157154
temp_filename = tempfile.mktemp()
158155
with open(temp_filename, 'w') as file_obj:
159156
same_blob.download_to_file(file_obj)
@@ -171,9 +168,7 @@ def test_copy_existing_file(self):
171168
new_blob = self.bucket.copy_blob(blob, self.bucket, 'CloudLogoCopy')
172169
self.case_blobs_to_delete.append(new_blob)
173170

174-
blob._reload_properties() # force a reload
175171
base_contents = blob.download_as_string()
176-
new_blob._reload_properties() # force a reload
177172
copied_contents = new_blob.download_as_string()
178173
self.assertEqual(base_contents, copied_contents)
179174

0 commit comments

Comments
 (0)