如何通过Python添加对某些服务帐户(特定的Google存储桶)的访问权限?

时间:2020-04-22 13:17:35

标签: python google-cloud-storage

当应自动创建存储桶时,我会自动执行。如果它不存在,它将自动创建并应用正确的IAM策略。

gsutil中,我可以通过以下方式实现:

gsutil acl ch -u john.doe@example.com:WRITE gs://example-bucket
gsutil acl ch -u john.doe@example.READ gs://example-bucket

问题是我不明白在Python中该怎么做。我浏览了GitHub,StackOverflow和官方文档,但看不到路。

我创建了一个存储桶client.create_bucket(bucket_name) based on the official python library and examples from there

1 个答案:

答案 0 :(得分:2)

我认为您应该使用google-cloud-storage

这是示例代码:


    client = storage.Client()
    bucket = client.get_bucket(bucket_name)
    acl = bucket.acl

    # For user
    acl.user("me@example.org").grant_read()
    acl.all_authenticated().grant_write()

    # For service account
    acl.service_account("example@example.iam.gserviceaccount.com").grant_read()

    acl.save()
    print(list(acl))