【发布时间】:2021-04-24 00:18:13
【问题描述】:
我正在尝试创建一个 API 以从 VM 实例内部管理 API 密钥,因此我正在寻找一种编程和/或使用 Google REST APIS 方式来创建、删除和列出 Google Cloud API 管理凭据,尤其是API 密钥。
使用 Google Cloud SDK,您可以使用以下命令 ("gcloud alpha services api-keys" docs) 创建 API 密钥:
$ gcloud alpha services api-keys create --display-name="test name"
通过谷歌云documentation仔细搜索后,我没有找到一种编程方式(示例1),也没有找到使用cURL(示例2)调用Google API来管理API密钥的方式。
以编程方式创建bucket的示例1:
from google.cloud import storage
def create_bucket_class_location(bucket_name):
"""Create a new bucket in specific location with storage class"""
# bucket_name = "your-new-bucket-name"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
bucket.storage_class = "COLDLINE"
new_bucket = storage_client.create_bucket(bucket, location="us")
print(
"Created bucket {} in {} with storage class {}".format(
new_bucket.name, new_bucket.location, new_bucket.storage_class
)
)
return new_bucket
使用 REST API 创建存储桶的示例 2:
curl -X POST --data-binary @create-bucket.json \
-H "Authorization: Bearer OAUTH2_TOKEN" \
-H "Content-Type: application/json" \
"https://storage.googleapis.com/storage/v1/b?project=PROJECT_ID"
【问题讨论】:
标签: python google-cloud-platform google-api