From a92f0c3cb7289fa47ec6463c565129d0140b0077 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Mon, 23 Mar 2015 11:41:41 -0700 Subject: [PATCH 1/2] Using copy and upload responses in storage. This prevents unnecessary reload_properties() calls for objects which should already have all metadata loaded. --- gcloud/storage/blob.py | 10 +++++++--- gcloud/storage/bucket.py | 3 ++- ...mb-file.zip => five-point-one-mb-file.zip} | Bin 5242880 -> 5253120 bytes regression/storage.py | 13 ++++--------- 4 files changed, 13 insertions(+), 13 deletions(-) rename regression/data/{five-mb-file.zip => five-point-one-mb-file.zip} (99%) diff --git a/gcloud/storage/blob.py b/gcloud/storage/blob.py index d5f2874d3e13..31ffb680a002 100644 --- a/gcloud/storage/blob.py +++ b/gcloud/storage/blob.py @@ -15,6 +15,7 @@ """Create / interact with Google Cloud Storage blobs.""" import copy +import json import mimetypes import os import time @@ -353,10 +354,13 @@ def upload_from_file(self, file_obj, rewind=False, size=None, # pass them as None, because apitools wants to print to the console # by default. if upload.strategy == transfer._RESUMABLE_UPLOAD: - upload.StreamInChunks(callback=lambda *args: None, - finish_callback=lambda *args: None) + http_response = upload.StreamInChunks( + callback=lambda *args: None, + finish_callback=lambda *args: None) else: - http_wrapper.MakeRequest(conn.http, request, retries=num_retries) + http_response = http_wrapper.MakeRequest(conn.http, request, + retries=num_retries) + self._properties = json.loads(http_response.content) def upload_from_filename(self, filename, content_type=None): """Upload this blob's contents from the content of a named file. diff --git a/gcloud/storage/bucket.py b/gcloud/storage/bucket.py index 67fec046514d..7422db121b5f 100644 --- a/gcloud/storage/bucket.py +++ b/gcloud/storage/bucket.py @@ -364,7 +364,8 @@ def copy_blob(self, blob, destination_bucket, new_name=None): new_name = blob.name new_blob = Blob(bucket=destination_bucket, name=new_name) api_path = blob.path + '/copyTo' + new_blob.path - self.connection.api_request(method='POST', path=api_path) + copy_result = self.connection.api_request(method='POST', path=api_path) + new_blob._properties = copy_result return new_blob def upload_file(self, filename, blob_name=None): diff --git a/regression/data/five-mb-file.zip b/regression/data/five-point-one-mb-file.zip similarity index 99% rename from regression/data/five-mb-file.zip rename to regression/data/five-point-one-mb-file.zip index 38da09a4f799185d92ee95fd290913a0b47dded0..a399081e8dd2e999e5051df2be42f583773e3d3f 100644 GIT binary patch delta 10488 zcmVGgaw2Kga?EOgb9QSgbRcWgbjoagb#!egb{=i zgcF1mgcXDqgcpPugc*bygd2n$gdKz)gdc<;gdv0?gd>C`ge8O~geQb3gein7ge!zB zge`giVA_gin7H9o~P^3$G&i5E*?XR-($&$ijK9D?AN?j&#*xJ(H@s z{{%9z@d9c$;LXHg04>!{XN5k#@wDu609Rw`y>3y@2f7+|{sOn^&w*q|le`0#Rr-CU zXZ?3uNt0pAfBZj3RlKiT&$@!}RYFV$6&{^@so0&mfGTKXinhxidsu(zTAj!u#Qj+6 z`_%zqgumSegemzS)M6sI=D*Oz-khdi4+M*S83O{K@w z-FtRv52ky|F@l)eedkL>pFZDpn-)dW)b`6_SF!j6i;UxWbR&Ptt6YNgIkm9F#57N{ zp;Ra`qtI~FRQ>4&RuRuB&$ueY-CpK4ejbddJ4+>gxc3nrL=@z^BsPH9Z|e9&OSTM*MsDGs`SQ=Xw`SkN_$QF z`up`sfU`H>96NtBdsf5?yn!-s+_Q2*7Yp)c1hGE9Euw#+bY83We`mC7ymjiN)sg$* z27!(Y4#N3czyFyj0?hKYAi25i6(*lE>`twuD1Dqy!_3o4orr-_T zQ@iLnJnV^imU|hB5bY?($Q(~Y4-Xnu)Gc8;EW3Y+gXl8RcAgN{_&BLV9q-%Ebo>@> z+AJNx5d0M}Q4p%P0+91*UpV70jbUUWuK=XUa#hX#bZ4L{OrViLp<@~Z8W@FR>0mxzHy%{FGQAC69r2DB zKs*l49W zg$?DQNZWmvcIL7wY^ln{2ZnRHJ|B;fjC+0Z4Rd1dS)Z&BiR7aF=MdVY%}_G}g@$X$ zODq4by#>#4?F^Bk>tDabw}jF9karOjr>~rF8=a|r=w4t-~h)jQ7{7xDX-0o?M&KVe`))F&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dvj-Li>0tu3+5WHN2+#Rk3M&{ z#>Qz&T2Snk-k9tLj2sAQr~jKrVP-B+g+(zmdh zgHS%NI6Y%x&4eS*ZB0Tz&!m4eDHB6)FJNkeA{+N=;!bR+0 zo3EG2;5<{E!?0B_;t<)0J5xm{qvx1e3EO)WG?of6=1U|A)k7iy58J*ZoToPm;D`2t zAg8juiu_qr6tW{N5Wn-{hu>#FTG|*B;9k4%3tOr7M+#ouPnO@kJS1jjI@Ve`bqR@iU4>eFlm9T$gT<&X@j{=r~UYnhRIB@-{E{s;65F)Kl;$YWN7ec8v u(W6#Bg7TmDmNnORJ1&$=it8bFI92bOE502In3Rp&&3kJIlho-3(Yb$cs6N^N delta 167 zcmWN=X;y&&006+3Hd+u0X+c!lY*|7jlp?y)_hSy`2py^By$OC-p&-8IdSUDxx9j+3ngWju3W3QaqG^#2URt74NZ?)+B&+P c^z;opd+}=M&B)lqyAM;JzRb*hTUh$}0}d5AX#fBK diff --git a/regression/storage.py b/regression/storage.py index 85919c517f74..7b467aebfe93 100644 --- a/regression/storage.py +++ b/regression/storage.py @@ -88,7 +88,7 @@ class TestStorageFiles(unittest2.TestCase): 'path': 'regression/data/CloudPlatform_128px_Retina.png', }, 'big': { - 'path': 'regression/data/five-mb-file.zip', + 'path': 'regression/data/five-point-one-mb-file.zip', }, 'simple': { 'path': 'regression/data/simple.txt', @@ -119,21 +119,19 @@ def test_large_file_write_from_stream(self): file_data = self.FILES['big'] with open(file_data['path'], 'rb') as file_obj: - self.bucket.upload_file_object(file_obj, blob_name=blob.name) + blob.upload_from_file(file_obj) self.case_blobs_to_delete.append(blob) - blob._reload_properties() # force a reload self.assertEqual(blob.md5_hash, file_data['hash']) def test_small_file_write_from_filename(self): - blob = storage.Blob(bucket=self.bucket, name='LargeFile') + blob = storage.Blob(bucket=self.bucket, name='SmallFile') self.assertEqual(blob._properties, {}) file_data = self.FILES['simple'] blob.upload_from_filename(file_data['path']) self.case_blobs_to_delete.append(blob) - blob._reload_properties() # force a reload self.assertEqual(blob.md5_hash, file_data['hash']) def test_write_metadata(self): @@ -143,7 +141,6 @@ def test_write_metadata(self): # NOTE: This should not be necessary. We should be able to pass # it in to upload_file and also to upload_from_string. blob.content_type = 'image/png' - blob._reload_properties() # force a reload self.assertEqual(blob.content_type, 'image/png') def test_direct_write_and_read_into_file(self): @@ -153,7 +150,7 @@ def test_direct_write_and_read_into_file(self): self.case_blobs_to_delete.append(blob) same_blob = storage.Blob(bucket=self.bucket, name='MyBuffer') - same_blob._reload_properties() # force a reload + same_blob._reload_properties() # Initialize properties. temp_filename = tempfile.mktemp() with open(temp_filename, 'w') as file_obj: same_blob.download_to_file(file_obj) @@ -171,9 +168,7 @@ def test_copy_existing_file(self): new_blob = self.bucket.copy_blob(blob, self.bucket, 'CloudLogoCopy') self.case_blobs_to_delete.append(new_blob) - blob._reload_properties() # force a reload base_contents = blob.download_as_string() - new_blob._reload_properties() # force a reload copied_contents = new_blob.download_as_string() self.assertEqual(base_contents, copied_contents) From 189e5fe137a84adbfd535dbef660f4362c9ad987 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Mon, 23 Mar 2015 11:53:04 -0700 Subject: [PATCH 2/2] Smoothing out unit tests to send valid responses. --- gcloud/storage/blob.py | 6 +++++- gcloud/storage/test_blob.py | 13 +++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/gcloud/storage/blob.py b/gcloud/storage/blob.py index 31ffb680a002..ac3f9ce86817 100644 --- a/gcloud/storage/blob.py +++ b/gcloud/storage/blob.py @@ -360,7 +360,11 @@ def upload_from_file(self, file_obj, rewind=False, size=None, else: http_response = http_wrapper.MakeRequest(conn.http, request, retries=num_retries) - self._properties = json.loads(http_response.content) + response_content = http_response.content + if not isinstance(response_content, + six.string_types): # pragma: NO COVER Python3 + response_content = response_content.decode('utf-8') + self._properties = json.loads(response_content) def upload_from_filename(self, filename, content_type=None): """Upload this blob's contents from the content of a named file. diff --git a/gcloud/storage/test_blob.py b/gcloud/storage/test_blob.py index 020276aed1dc..b4dd01355d11 100644 --- a/gcloud/storage/test_blob.py +++ b/gcloud/storage/test_blob.py @@ -348,7 +348,7 @@ def _upload_from_file_simple_test_helper(self, properties=None, DATA = b'ABCDEF' response = {'status': OK} connection = _Connection( - (response, b''), + (response, b'{}'), ) bucket = _Bucket(connection) blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) @@ -412,10 +412,11 @@ def test_upload_from_file_resumable(self): chunk1_response = {'status': http_wrapper.RESUME_INCOMPLETE, 'range': 'bytes 0-4'} chunk2_response = {'status': OK} + # Need valid JSON on last response, since resumable. connection = _Connection( (loc_response, b''), (chunk1_response, b''), - (chunk2_response, b''), + (chunk2_response, b'{}'), ) bucket = _Bucket(connection) blob = self._makeOne(BLOB_NAME, bucket=bucket) @@ -470,7 +471,7 @@ def test_upload_from_file_w_slash_in_name(self): 'range': 'bytes 0-4'} chunk2_response = {'status': OK} connection = _Connection( - (loc_response, ''), + (loc_response, '{}'), (chunk1_response, ''), (chunk2_response, ''), ) @@ -512,7 +513,7 @@ def _upload_from_filename_test_helper(self, properties=None, 'range': 'bytes 0-4'} chunk2_response = {'status': OK} connection = _Connection( - (loc_response, ''), + (loc_response, '{}'), (chunk1_response, ''), (chunk2_response, ''), ) @@ -576,7 +577,7 @@ def test_upload_from_string_w_bytes(self): 'range': 'bytes 0-4'} chunk2_response = {'status': OK} connection = _Connection( - (loc_response, ''), + (loc_response, '{}'), (chunk1_response, ''), (chunk2_response, ''), ) @@ -614,7 +615,7 @@ def test_upload_from_string_w_text(self): 'range': 'bytes 0-4'} chunk2_response = {'status': OK} connection = _Connection( - (loc_response, ''), + (loc_response, '{}'), (chunk1_response, ''), (chunk2_response, ''), )