diff --git a/storage/google/cloud/storage/blob.py b/storage/google/cloud/storage/blob.py index 3ed778bde062..c99555277e95 100644 --- a/storage/google/cloud/storage/blob.py +++ b/storage/google/cloud/storage/blob.py @@ -868,11 +868,14 @@ def rewrite(self, source, token=None, client=None): method='POST', path=source.path + '/rewriteTo' + self.path, query_params=query_params, data=self._properties, headers=headers, _target_object=self) - self._set_properties(api_response['resource']) rewritten = int(api_response['totalBytesRewritten']) size = int(api_response['objectSize']) + # The resource key is set if and only if the API response is + # completely done. Additionally, there is no rewrite token to return + # in this case. if api_response['done']: + self._set_properties(api_response['resource']) return None, rewritten, size return api_response['rewriteToken'], rewritten, size diff --git a/storage/unit_tests/test_blob.py b/storage/unit_tests/test_blob.py index d0de26ef3bbc..45bfc901ee32 100644 --- a/storage/unit_tests/test_blob.py +++ b/storage/unit_tests/test_blob.py @@ -1288,6 +1288,33 @@ def test_compose_w_additional_property_changes(self): self.assertEqual(kw[0]['path'], '/b/name/o/%s/compose' % DESTINATION) self.assertEqual(kw[0]['data'], SENT) + def test_rewrite_response_without_resource(self): + from six.moves.http_client import OK + + SOURCE_BLOB = 'source' + DEST_BLOB = 'dest' + DEST_BUCKET = 'other-bucket' + TOKEN = 'TOKEN' + RESPONSE = { + 'totalBytesRewritten': 33, + 'objectSize': 42, + 'done': False, + 'rewriteToken': TOKEN, + } + response = ({'status': OK}, RESPONSE) + connection = _Connection(response) + client = _Client(connection) + source_bucket = _Bucket(client=client) + source_blob = self._make_one(SOURCE_BLOB, bucket=source_bucket) + dest_bucket = _Bucket(client=client, name=DEST_BUCKET) + dest_blob = self._make_one(DEST_BLOB, bucket=dest_bucket) + + token, rewritten, size = dest_blob.rewrite(source_blob) + + self.assertEqual(token, TOKEN) + self.assertEqual(rewritten, 33) + self.assertEqual(size, 42) + def test_rewrite_other_bucket_other_name_no_encryption_partial(self): from six.moves.http_client import OK