Skip to content Skip to sidebar Skip to footer

Provide A User Project To Google Cloud Storage Bucket

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:

Solution 1:

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.

Post a Comment for "Provide A User Project To Google Cloud Storage Bucket"