Skip to content

Commit 486163e

Browse files
lucmultlukesneeringer
authored andcommitted
Fixes #3150 - Ignores resource key when it isn't present (#3177)
1 parent e28ce1b commit 486163e

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

storage/google/cloud/storage/blob.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,11 +868,14 @@ def rewrite(self, source, token=None, client=None):
868868
method='POST', path=source.path + '/rewriteTo' + self.path,
869869
query_params=query_params, data=self._properties, headers=headers,
870870
_target_object=self)
871-
self._set_properties(api_response['resource'])
872871
rewritten = int(api_response['totalBytesRewritten'])
873872
size = int(api_response['objectSize'])
874873

874+
# The resource key is set if and only if the API response is
875+
# completely done. Additionally, there is no rewrite token to return
876+
# in this case.
875877
if api_response['done']:
878+
self._set_properties(api_response['resource'])
876879
return None, rewritten, size
877880

878881
return api_response['rewriteToken'], rewritten, size

storage/unit_tests/test_blob.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,33 @@ def test_compose_w_additional_property_changes(self):
12881288
self.assertEqual(kw[0]['path'], '/b/name/o/%s/compose' % DESTINATION)
12891289
self.assertEqual(kw[0]['data'], SENT)
12901290

1291+
def test_rewrite_response_without_resource(self):
1292+
from six.moves.http_client import OK
1293+
1294+
SOURCE_BLOB = 'source'
1295+
DEST_BLOB = 'dest'
1296+
DEST_BUCKET = 'other-bucket'
1297+
TOKEN = 'TOKEN'
1298+
RESPONSE = {
1299+
'totalBytesRewritten': 33,
1300+
'objectSize': 42,
1301+
'done': False,
1302+
'rewriteToken': TOKEN,
1303+
}
1304+
response = ({'status': OK}, RESPONSE)
1305+
connection = _Connection(response)
1306+
client = _Client(connection)
1307+
source_bucket = _Bucket(client=client)
1308+
source_blob = self._make_one(SOURCE_BLOB, bucket=source_bucket)
1309+
dest_bucket = _Bucket(client=client, name=DEST_BUCKET)
1310+
dest_blob = self._make_one(DEST_BLOB, bucket=dest_bucket)
1311+
1312+
token, rewritten, size = dest_blob.rewrite(source_blob)
1313+
1314+
self.assertEqual(token, TOKEN)
1315+
self.assertEqual(rewritten, 33)
1316+
self.assertEqual(size, 42)
1317+
12911318
def test_rewrite_other_bucket_other_name_no_encryption_partial(self):
12921319
from six.moves.http_client import OK
12931320

0 commit comments

Comments
 (0)