简体   繁体   中英

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.

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'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM