Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Google Cloud storage : bucket.patch() gives invalid arguments error

I want to update labels using the python script and found this code google cloud documentation

import pprint

from google.cloud import storage


def add_bucket_label(bucket_name):
    # bucket_name = "your-bucket-name"

    storage_client = storage.Client()

    bucket = storage_client.get_bucket(bucket_name)
    labels = bucket.labels
    labels["example"] = "label"
    bucket.labels = labels
    bucket.patch()

    print("Updated labels on {}.".format(bucket.name))
    pprint.pprint(bucket.labels)

but it is giving "invalid arguments" error

400 PUT https://storage.googleapis.com/storage/.../{bucket_name}?projection=full&prettyPrint=false: Invalid argument.
0 3 664
3 REPLIES 3

glen_yu
Google Developer Expert
Google Developer Expert

It's working fine for me.  Do you have the correct perms?   And I assume you have the pip package "google-cloud-storage" installed?

 

 

#! /usr/bin/env python3
import pprint

from google.cloud import storage


def add_bucket_label(bucket_name):
    """Add a label to a bucket."""

    storage_client = storage.Client()

    bucket = storage_client.get_bucket(bucket_name)
    labels = bucket.labels
    labels["example"] = "label2"
    bucket.labels = labels
    bucket.patch()

    print("Updated labels on {}.".format(bucket.name))
    pprint.pprint(bucket.labels)


Running it i.e calling add_bucket_label('my-gcs-bucket') produced the output:

Updated labels on my-gcs-bucket.
{'example': 'label2'}

 

yes i have it installed but don't know why its showing this error.
But i think if i change "label2" to a string variable i.e. bucket_name then it doesn't work.

Nevermind i just tried to label it manually on the site it turned out I can't use camelcase for the label key.
Thanks you for your time.