我可以在 Google Cloud 上创建存储桶,但无法选择存储类别 {Multi-regional、Regional、Nearline、Coldline} 或位置 {'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 都显示存储类和位置选项的参数。
最佳答案
将代码更改为:
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 的 Google Cloud 客户端库的文档 here ,向您展示“Bucket”类的方法和属性。
关于python - 如何选择 Google Cloud Storage 存储桶的存储类别和位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53462523/