Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

python 3.x - Google cloud - delete bucket metadata

trying to delete metadata on an object in a google cloud bucket.

the docs only so an example when using the json api, in which is says to send metadata-key:null.

I have tried this using python below, but all this does is replace metadata-data with "null" as a string instead of deleting it.

how do you actually delete metadata using the api/gsutil

i have tried gsutil setmeta also, its easy to edit the metadata, but i cant seem to delete it..

https://cloud.google.com/storage/docs/viewing-editing-metadata

import os f

from google.cloud import storage

    bucket_name = "bucket-name"
    blob_name = "blobname"
    
    
    
    
    def set_blob_metadata(bucket_name, blob_name):
    
        storage_client = storage.Client()
        bucket = storage_client.bucket(bucket_name)
        blob = bucket.get_blob(blob_name)
        metadata = {'x-goog-meta-key':"null"}
        blob.metadata = metadata
        blob.patch()
    
        print("The metadata for the blob {} is {}".format(blob.name, blob.metadata))


set_blob_metadata(bucket_name, blob_name)
question from:https://stackoverflow.com/questions/65899926/google-cloud-delete-bucket-metadata

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In order to delete custom metadata with the gsutil setmeta command you'd simply need to provide a null value to the relevant key. E.g. if you have a custom metadata names key with a value foo running the following command should delete the custom metadata:

gsutil setmeta -h "x-goog-meta-key:" gs://[YOUR-BUCKET]/[YOUR-OBJECT]

Take into consideration that as advised on the link I shared above:

Custom metadata is always prefixed in gsutil with x-goog-meta-. This distinguishes it from standard request headers. Other tools that send and receive object metadata by using the request body do not use this prefix.

Additionally you can delete the metadata fairly easily by clicking on the trashcan symbol of the Edit Metadata option on the Cloud Console.

EDIT

The following script should remove the metadata using the Python Client Library:

from google.cloud import storage

bucket_name = "[YOUR-BUCKET-NAME]"
blob_name = "[YOUR-OBJECT-NAME]"
key = "[YOUR-CUSTOM-METADATA-KEY-NAME]" #e.g. if you have a Custom Metadata named "key" with a value "foo" just enter key

def remove_blob_metadata(bucket_name, blob_name, key):

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.get_blob(blob_name)

    if blob.metadata != None:
        if key not in blob.metadata.keys():
            print("Couldn't find that key under the Custom Metadata field for object {} in bucket {}".format(blob_name,bucket_name))
        else:
            d = blob.metadata
            blob.metadata = None
            blob.patch()
            del d[key]
            blob.metadata = d
            blob.patch()
            print("Removed "{}" key from Custom Metadata field for object {} in bucket {}".format(key,blob_name,bucket_name))
    else:
        print("Object {} in bucket {} doesn't have Custom Metadata".format(blob_name,bucket_name))

remove_blob_metadata(bucket_name, blob_name, key)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...