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

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

问题描述

我使用Google云端存储来存储我的文件。
我有我的存储桶,我可以存储我的图像,但是当我将图像的URL放入href时,我有这样的信息:

 <错误> 
< Code> UserProjectMissing< / Code>
<讯息>
桶是一个请求者支付桶,但没有提供用户项目。
< / Message>
< / Error>

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


$ b $ (BUCKET_NAME)
blob = bucket.blob(b $)文件名)
blob.upload_from_string(file_stream,content_type = content_type)
url = blob.public_url

所以我想知道如何提供一个用户项目。 解决方案

Python代码示例向您展示了如何提供付款在您的代码中,调用 storage.Client(project = PROJECT_ID)时调用您的请求。



通过桶的项目, client.bucket(BUCKET_NAME)不会告诉存储区哪个项目必须为此请求付费。对于 Requester支付的存储桶,您需要在获取存储桶时提供一个项目。这可以是同一个项目 PROJECT_ID 或不同的项目,例如 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(文件名)
blob.upload_from_string(file_stream,content_type = content_type)
url = blob.public_url

您可以为每个将要为存储桶请求付费的存储桶例如获取图片或上传图片)。

默认情况下,存储桶的项目已付费(通过收取与此项目关联的结算帐户) 。我认为这是最常见的设置。



或者, Requester支付意味着由请求引起的一些成本是由包含在请求中的结算项目支付。这种设置对于多个项目(例如大公司中的不同公司或部门)共享同一个存储桶但单独收取部分成本的使用案例是有意义的。 (尽管存储费用和提前删除费用通常都会收到存储桶的项目)



有关云存储的GCP文档中的更多信息:请求者支付



我假设你无意中开启了请求者支付。您可以在Cloud Console中打开/关闭 Requester pays ,如下所示:


  1. 切换到项目您的存储分区


  2. 导航到云存储>浏览器以查看存储分区列表

  3. $ b
  4. 在存储分区的 Requester支付列中,一个按钮会告诉您它是还是。点击该按钮并确认在两种状态之间切换。

  5. 如果您关闭请求者支付,您的代码应该按原样运行。


    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.

    解决方案

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

    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)

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

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

    1. Switch to the project of your bucket

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

    3. 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.

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

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

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