如何为Google Cloud Storage存储桶选择存储类别和位置

时间:2018-11-24 21:30:07

标签: python google-cloud-storage

我能够在Google Cloud上创建存储分区,但无法选择存储类别{多区域,区域,近线,冷线}或位置{'us-west1'等)。

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)

current documentation在Python中没有显示bucket_name以外的任何参数;但是,Go,Java,Node.JS和Ruby都显示了存储类和位置选项的参数。

1 个答案:

答案 0 :(得分:5)

将代码更改为此:

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)

您可以找到适用于Python here的Google Cloud Client库的文档,其中向您展示了“桶”类的方法和属性。