Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't delete custom metadata key on object #381

Closed
hankd-v opened this issue Feb 16, 2021 · 2 comments · Fixed by #383
Closed

Can't delete custom metadata key on object #381

hankd-v opened this issue Feb 16, 2021 · 2 comments · Fixed by #383
Assignees
Labels
api: storage Issues related to the googleapis/python-storage API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@hankd-v
Copy link

hankd-v commented Feb 16, 2021

I ran into some issues trying to delete a custom metadata key from a storage object. I expected the following to work, but did not result in the key being deleted:

# blob has metadata with key 'foo'
new_metadata = {}
for x in blob.metadata:
    if x != 'foo':
        new_metadata[x] = blob.metadata[x]
# new_metadata is now a copy of the metadata without key 'foo'
blob.metadata = new_metadata
blob.patch()
# The blob's metadata still has key 'foo'

The Rest API indicated to send a value of JSON null associated with the key to perform a deletion, but that doesn't seem to happen when issuing a blob.patch with a metadata with a key removed.

As a work around, I created a copy of the desired metadata, deleted all of the blob's metadata (which does work), then updated the metadata to be the desired metadata.

new_metadata = {}
for x in blob.metadata:
    if x != 'foo':
        new_metadata[x] = blob.metadata[x]
blob.metadata = None
blob.patch()
blob.metadata = new_metadata
blob.patch()
@product-auto-label product-auto-label bot added the api: storage Issues related to the googleapis/python-storage API. label Feb 16, 2021
@tritone tritone added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Feb 16, 2021
@tritone tritone self-assigned this Feb 16, 2021
@tritone
Copy link
Contributor

tritone commented Feb 17, 2021

I was able to reproduce the issue from your example.

I think it's expected that deleting a key would not succeed in overwriting the key when using a patch request. However, I would think that at least by setting a key's value to None, it would be possible to indicate that the key should be deleted by sending null to the API. However, this also fails because of this line here:

value = {k: str(v) for k, v in value.items()}
-- converting the value to a string means that None is stored as "None"!

I tested locally and by rewriting the line as follows, we can allow keys to be deleted by setting their value to None:

            value = {k: str(v) if v is not None else None for k, v in value.items()}

@frankyn @andrewsg does this seem like the correct approach to you? If so I'll make a PR (and add it to the docs for metadata that this is how keys can be deleted).

tritone added a commit to tritone/python-storage that referenced this issue Feb 17, 2021
Metadata keys can be cleared in the JSON API by setting the value
to null; however this doesn't currently work through this library.
This PR allows users to clear metadata keys by setting the value
to None and documents how users should do this.

Fixes googleapis#381
@frankyn
Copy link
Member

frankyn commented Feb 17, 2021

I'm surprised we hadn't seen this before to be honest, but yes, the suggestion LGTM!

tritone added a commit that referenced this issue Feb 17, 2021
Metadata keys can be cleared in the JSON API by setting the value
to null; however this doesn't currently work through this library.
This PR allows users to clear metadata keys by setting the value
to None and documents how users should do this.

Fixes #381
cojenco pushed a commit to cojenco/python-storage that referenced this issue Oct 13, 2021
Metadata keys can be cleared in the JSON API by setting the value
to null; however this doesn't currently work through this library.
This PR allows users to clear metadata keys by setting the value
to None and documents how users should do this.

Fixes googleapis#381
cojenco pushed a commit to cojenco/python-storage that referenced this issue Oct 13, 2021
Metadata keys can be cleared in the JSON API by setting the value
to null; however this doesn't currently work through this library.
This PR allows users to clear metadata keys by setting the value
to None and documents how users should do this.

Fixes googleapis#381
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: storage Issues related to the googleapis/python-storage API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
3 participants