Skip to content

blob.py is unable to handle a partial rewrite and fails with KeyError on lack of resource  #3150

@80nashi

Description

@80nashi

https://github.com/GoogleCloudPlatform/google-cloud-python/blob/master/storage/google/cloud/storage/blob.py#L871

871:        self._set_properties(api_response['resource'])

This relies on the API to always return 'resource'. However, it is optional as per the doc.
https://cloud.google.com/storage/docs/json_api/v1/objects/rewrite
"""
This property is present in the response only when copying completes.
"""

As such, when the write was partial, done: false is returned and resource not present. Thus failing with the following KeyError.

Traceback (most recent call last): 
File "xxx.py", line 304, in test_011_CopyFileFromBucketToBucketNewName 
new_destination_file_name=new_file_name) 
File "xxx.py, line 276, in copyFileFromBucketToBucket 
token, rewritten, total = destination_blob.rewrite(source_blob, token=token) 
File "/home/jenkins/venv/lib/python2.7/site-packages/google/cloud/storage/blob.py", line 681, in rewrite 
self._set_properties(api_response['resource']) 
KeyError: 'resource' 

The most easy fix would be to push _set_properties down under after done: false check:

E.g.;

       api_response = client._connection.api_request(
            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'])  ## Comment out this line
        rewritten = int(api_response['totalBytesRewritten'])
        size = int(api_response['objectSize'])

        if api_response['done']:
            return None, rewritten, size

        self._set_properties(api_response['resource']) # and move here 
        return api_response['rewriteToken'], rewritten, size

Metadata

Metadata

Labels

api: storageIssues related to the Cloud Storage API.priority: p2Moderately-important priority. Fix may not be included in next release.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions