Ryan Loggerythm
Ryan Loggerythm

Reputation: 3314

How to choose storage class and location for Google Cloud Storage buckets

I'm able to create storage buckets on Google Cloud, but I'm not able to choose the storage class {Multi-regional, Regional, Nearline, Coldline} or the location {'us-west1', etc}.

from google.cloud import storage    

def CreateBucket(name):
    try:
        storageClient = storage.Client()
        bucket = storageClient.create_bucket(name)
        print(f'Bucket {bucket.name} created.')
    except Exception as ex:
        print(f'exception!\n{ex}')


name = 'my_globally_unique_bucket_name'
CreateBucket(name)

The current documentation does not show any parameters beyond bucket_name in Python; however, Go, Java, Node.JS, and Ruby all show parameters for the storage class and location options.

Upvotes: 2

Views: 547

Answers (1)

Maxim
Maxim

Reputation: 4431

Change the code to this:

from google.cloud import storage

def CreateBucket(name):
        try:
            storageClient = storage.Client()
            bucket = storageClient.bucket(name)
            bucket.location = "us-west1"
            bucket.storage_class = "COLDLINE"
            bucket.create()
            print("Bucket {} created.".format(name))
        except Exception as ex:
            print("exception!\n{}".format(ex))

name = 'my_globally_unique_bucket_name'
CreateBucket(name)

You can find the documentation for Google Cloud Client Library for Python here, showing you the methods and attributes of the Class 'Bucket'.

Upvotes: 4

Related Questions