GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — blob.py is unable to handle a partial rewrite and fails with KeyError on lack of resource · Issue #3150 · googleapis/google-cloud-python

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

New issue
Copy link
New issue
Copy link
Closed
Closed
blob.py is unable to handle a partial rewrite and fails with KeyError on lack of resource #3150
Copy link
Assignees
lukesneeringer
Labels
api: storageIssues related to the Cloud Storage API.priority: p2Moderately-important priority. Fix may not be included in next release.

Description

@80nashi
80nashi
opened on Mar 15, 2017
Issue body actions

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

Activity

  1. verbal commented on Mar 16, 2017

    @verbal
    verbal
    on Mar 16, 2017
    More actions

    I don't think your solution will work. I came across the same issue, but when it is not done, it falls through. You probably want to move it inside the if api_response['done'].

  2. 80nashi commented on Mar 16, 2017

    @80nashi
    80nashi
    on Mar 16, 2017
    Author
    More actions

    Ah, you are totally right. Scrap my suggestion.
    I wasn't sure how _set_properties is used in the API and I mistook the "done" check in the wrong way, of course.

  3. daspecster
    added
    api: storageIssues related to the Cloud Storage API.
    on Mar 16, 2017
  4. daspecster commented on Mar 16, 2017

    @daspecster
    daspecster
    on Mar 16, 2017
    Contributor
    More actions

    Thanks for reporting @80nashi!

    If I understand correctly from the docs, the resource is only available when the copy is complete?

    A resource containing the metadata for the copied-to object. This property is present in the response only when copying completes.

    @tseaver, @dhermes maybe we should be raising an exception here?

    I'm not sure from the docs, but is there polling that should be available for rewriting so you can check if the copy is done?

  5. lukesneeringer
    added
    priority: p2Moderately-important priority. Fix may not be included in next release.
    status: acknowledged
    on Mar 16, 2017
  6. 80nashi commented on Mar 17, 2017

    @80nashi
    80nashi
    on Mar 17, 2017
    Author
    More actions

    As I understand the method and the API, the method is expected to return the rewrite token without raising an exception, in case of partial write (done: false and resource non-existent).
    Then the user program can call the method again with the non-null, non empty string rewrite token, repeat it until done: true and resource present.

  7. 80nashi commented on Mar 17, 2017

    @80nashi
    80nashi
    on Mar 17, 2017
    Author
    More actions

    So yeah, the following can be a possible fix as verba suggested.
    However, I'm not sure what _set_properties is expected to do in case of done: false.

            # self._set_properties(api_response['resource'])  ## Comment out this line
            rewritten = int(api_response['totalBytesRewritten'])
            size = int(api_response['objectSize'])
    
            if api_response['done']:
                self._set_properties(api_response['resource']) # and move here
                return None, rewritten, size
    
            return api_response['rewriteToken'], rewritten, size
    
  8. lucmult
    added a commit that references this issue on Mar 21, 2017

    Fixes googleapis#3150 - Ignores resource key when it isn't present

    This issue will close once commit c025336 is merged into the 'master' branch.
    c025336
  9. lukesneeringer
    closed this as completedin 486163eon Mar 21, 2017
  10. daspecster commented on Mar 21, 2017

    @daspecster
    daspecster
    on Mar 21, 2017
    Contributor
    More actions

    @80nashi if you want to try and install from master it appears that @lukesneeringer addressed this.

  11. richkadel
    added a commit that references this issue on May 6, 2017

    Fixes googleapis#3150 - Ignores resource key when it isn't present (g…

    This issue will close once commit 4a422f3 is merged into the 'master' branch.
    4a422f3
  12. JustinBeckwith
    assigned
    lukesneeringer
    on Feb 1, 2021
  13. parthea
    added a commit that references this issue on Mar 9, 2026

    Fixes #3150 - Ignores resource key when it isn't present (#3177)

    This issue will close once commit 488771f is merged into the 'main' branch.
    488771f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @lukesneeringer
    lukesneeringer

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

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.