【问题标题】:Are legacy Cloud storage permissions required?是否需要旧版云存储权限?
【发布时间】:2018-07-19 11:02:18
【问题描述】:

我的困惑是为什么我需要包含“传统”云存储角色。我更喜欢避免说“遗留”的东西,因为听起来它们会在这些日子里被弃用。 我做错了吗?

这是我的情况:

我正在使用 appengine 项目中的服务帐户来访问另一个项目中云存储中的文件。我正在使用 Google Python 客户端访问数据。

我已分配角色:

Storage Object Creator
Storage Object Viewer

但是当我尝试访问文件时出现错误:

<service account> does not have storage.buckets.get access

只有在我添加“遗留角色”后,它才能最终获得访问权限:

Storage legacy bucket writer
Storage legacy bucket reader

代码如下:

def download_blob(bucket_name, source_blob_name, destination_file_name):
    """Downloads a blob from the bucket."""
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(source_blob_name)
    blob.download_to_filename(destination_file_name)

    print('Blob {} downloaded to {}.'.format(
          source_blob_name,
          destination_file_name))

def upload_blob(bucket_name, source_file_name, destination_blob_name):
    """Uploads a file to the bucket."""
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(destination_blob_name)
    blob.upload_from_filename(source_file_name)

    print('File {} uploaded to {}.'.format(
          source_file_name,
          destination_blob_name))

谢谢 抢

【问题讨论】:

  • 你好 Rob,你能提供你正在使用的代码吗?
  • @FrankNatividad 添加。

标签: google-app-engine google-cloud-storage


【解决方案1】:

W.r.t 到您的代码,我在下面添加了额外的 cmets:

def download_blob(bucket_name, source_blob_name, 
destination_file_name):
    """Downloads a blob from the bucket."""
    """The following .get_bucket() requires storage.buckets.get permission."""
    bucket = storage_client.get_bucket(bucket_name)
    """The following doesn't"""
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(source_blob_name)
    blob.download_to_filename(destination_file_name)

    print('Blob {} downloaded to {}.'.format(
          source_blob_name,
          destination_file_name))

重申:

storage_client.get_bucket(bucket_name) 需要 storage.bucket.get 的权限,因为它正在执行存储桶元数据 GET 请求。

storage_cilent.bucket(bucket_name) 不需要此权限,因为它不执行 GET 请求,仅创建名称由 bucket_name 定义的存储桶对象。

上传绕过 storage.buckets.get 问题:

from google.cloud import storage
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(source_blob_name)
blob.upload_from_filename(source_file_name)

【讨论】:

  • 谢谢。这对我来说似乎很奇怪。在这两种情况下,我都是从同一个存储桶中读取的,所以我不明白为什么它不适用于 get_bucket。不过谢谢。
  • 感谢您的反馈。我想更好地回答这个问题,以便您理解。 get_bucket 对桶元数据资源执行获取请求。当您不想这样做时,只需下载文件。因此,您将使用 bucket 创建对带有名称的存储桶的引用,但不对存储桶元数据执行 GET 请求。
  • @frank-natividad 我有一个类似的问题,这并没有向我解释。如果我给服务帐户 Storage Legacy Bucket Reader 和 Storage Legacy Bucket Writer 角色,我们的代码会按预期写入,但如果我给它 Storage Object Creator 角色,它会失败并显示&lt;service account&gt; does not have storage.buckets.get access to &lt;bucket&gt;。没有可以写的非传统、非管理员角色吗?
  • Python 客户端库有一种方法来初始化 Bucket 的实例,无论是否从 GCS 请求元数据。 w.r.t 从 GCS 请求元数据 (storage.get_bucket()) 如果从 GCS 执行元数据请求,则需要 storage.buckets.get。 w.r.t 不向 GCS 请求元数据 (storage.bucket())。该方法仅初始化存储桶的实例,不请求元数据。这允许您跳过需要权限storage.buckets.get,因为没有检索到元数据。
  • @FrankNatividad 我不想绕过元数据请求。我要求一个可以写作的非传统、非管理员角色。在我看来,这是一个具有存储对象创建者权限的服务帐户无法写入的错误。
猜你喜欢
  • 2021-04-04
  • 1970-01-01
  • 2021-12-21
  • 2016-08-23
  • 1970-01-01
  • 1970-01-01
  • 2016-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多