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
Closed

Can't delete custom metadata key on object #381

hankd-v opened this issue Feb 16, 2021 · 2 comments

Comments

@hankd-v
Copy link

@hankd-v 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()
@tritone
Copy link
Contributor

@tritone 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 frankyn commented Feb 17, 2021

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

@tritone tritone closed this in #383 Feb 17, 2021
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 added 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 added 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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants