向 Google Cloud Storage 存储分区提供用户项目 [英] Provide a user project to Google Cloud Storage bucket

查看:40
本文介绍了向 Google Cloud Storage 存储分区提供用户项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Google Cloud Storage 来存储我的文件.我有我的存储桶,我可以存储我的图像,但是当我将图像的 url 放在一个 href 中时,我有这个消息:

I use Google Cloud Storage to store my files. I have my bucket, I can store my images but when I put the url of my image in a href I have this message:

<Error>
  <Code>UserProjectMissing</Code>
  <Message>
       Bucket is a requester pays bucket but no user project provided.
  </Message>
</Error>

这是我访问我的存储桶的代码:

This is my code to access to my bucket :

client = storage.Client(project=PROJECT_ID).from_service_account_json(SERVICE_ACCOUNT_FILE)
bucket = client.bucket(BUCKET_NAME)
blob = bucket.blob(filename)
blob.upload_from_string(file_stream, content_type=content_type)
url = blob.public_url

所以我想知道如何提供一个用户项目.

So I want to know how to provide a user project.

推荐答案

这个 Python 代码示例 向您展示如何在 Python 中根据您的请求提供付费项目.

This Python code sample shows you how to provide the paying project to your request in Python.

虽然在您的代码中,storage.Client(project=PROJECT_ID) 是与存储桶的项目一起调用的,但 client.bucket(BUCKET_NAME) 不会告诉存储哪个项目必须为此请求支付费用.对于Requester pays的bucket,获取bucket时还需要提供project.这可以是同一个项目 PROJECT_ID 或不同的项目,例如REQUESTERS_PROJECT_ID.

While in your code, storage.Client(project=PROJECT_ID) is called with the project of the bucket, client.bucket(BUCKET_NAME) doesn't tell the bucket which project has to pay for this request. For a bucket with Requester pays, you need to also provide a project when getting the bucket. This can be the same project PROJECT_ID or a different project, e.g. REQUESTERS_PROJECT_ID.

client = storage.Client(project=PROJECT_ID).from_service_account_json(SERVICE_ACCOUNT_FILE)
bucket = client.bucket(BUCKET_NAME, REQUESTERS_PROJECT_ID)
blob = bucket.blob(filename)
blob.upload_from_string(file_stream, content_type=content_type)
url = blob.public_url

您可以为每个将对存储桶的请求(例如获取图像或上传图像)计费的存储桶进行配置.

You can configure for every bucket who is going to be billed for requests to the bucket (e.g. getting an image or uploading an image).

默认是对该桶的项目进行计费,(通过与该项目关联的计费账户进行计费).我认为这是最常见的设置.

The default is that the project of the bucket is billed, (by charging the billing account that is associated with this project). I would think that this is the most common setup.

或者,请求者支付意味着由请求引起的部分费用由请求中包含的计费项目支付.这种设置在多个项目(例如大公司中的不同公司或部门)共享同一个存储桶但对某些成本单独计费的用例中可能有意义.(不过,存储费用和提前删除费用始终计入存储桶的项目)

Alternatively, Requester pays means that some of the costs that are caused by a request is paid by a billing project that is included in the request. This setup might make sense in use-cases where multiple projects (such as different companies or departments in a big corporation) share the same bucket, but are billed separately for some of the costs. (Storage charges and early deletion charges are always billed to the bucket's project though)

Cloud Storage 的 GCP 文档中的更多信息:请求者付款.

More information in the GCP docs of Cloud Storage: Requester Pays.

我认为您无意中打开了请求者付费.您可以在 Cloud Console 中打开/关闭 Requester pay,如下所示:

I assume that you unintentionally turned on Requester pays. You can turn on/off Requester pays in Cloud Console as following:

  1. 切换到你存储桶的项目

  1. Switch to the project of your bucket

导航到 Cloud Storage > 浏览器 以查看存储桶列表

Navigate to Cloud Storage > Browser to see a list of buckets

在桶的 Requester pays 列中,有一个按钮告诉您它是 On 还是 Off.单击该按钮并确认以在两种状态之间切换.

In the column Requester pays of the bucket, a button tells you if it is On or Off. Click on that button and confirm to toggle between the two states.

如果您关闭 Requester pays,您的代码应该可以正常工作.

If you turn off Requester pays, your code should work as is.

这篇关于向 Google Cloud Storage 存储分区提供用户项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆