Generate Cloud Storage signed URL from Google Cloud Function without using explicit key file


Istrupin

I want to create a presigned upload URL to the bucket and want to avoid explicitly referencing the json key.

Currently I'm trying to do this with the default App Engine service account

I am trying to follow this answer but I am getting this error:

AttributeError: You need a private key to sign the credential. The <class 'google.auth.compute_engine.credentials.Credentials'> credential you are currently using contains only a token. See https://googleapis.dev/python/google-api-core/latest/auth.html#setting-up-a-service-account for more details .

My Cloud Function code looks like this:

from google.cloud import storage
import datetime
import google.auth

def generate_upload_url(blob_name, additional_metadata: dict = {}):
    credentials, project_id = google.auth.default()
    # Perform a refresh request to get the access token of the current credentials (Else, it's None)
    from google.auth.transport import requests

    r = requests.Request()
    credentials.refresh(r)

    client = storage.Client()
    bucket = client.get_bucket("my_bucket")
    blob = bucket.blob(blob_name)
    
    service_account_email = credentials.service_account_email
    print(f"attempting to create signed url for {service_account_email}")
    url = blob.generate_signed_url(
        version="v4",
        service_account_email=service_account_email,
        access_token=credentials.token,
        # This URL is valid for 120 minutes
        expiration=datetime.timedelta(minutes=120),
        # Allow PUT requests using this URL.
        method="PUT",
        content_type="application/octet-stream",
        
    )
    return url


def get_upload_url(request):
    blob_name = get_param(request, "blob_name")
    url = generate_upload_url(blob_name)
    return url
Guillaume Braquere

When you're using the v4 version of the signed URL, the first line of the method calls the ensure_signed_credentialsmethod, which checks if the current service account can generate a signature in standalone mode (hence using the private key). So this breaks the current behavior.

In the comments of the function, it is clearly described that the service account JSON file is required

        If you are on Google Compute Engine, you can't generate a signed URL.
        Follow `Issue 922`_ for updates on this. If you'd like to be able to
        generate a signed URL from GCE, you can use a standard service account
        from a JSON file rather than a GCE service account.

So use the v2 version instead.

Related


Upload file to Google Cloud Storage signed URL using Python

Raj Chaudhary Can someone help me with the code, preferably using the Requests library to upload a file to a Google Cloud Storage signed URL in Python? Lundin Custer You can check out this sample code on Github . It shows a portable way to achieve this using P

Upload file to Google Cloud Storage signed URL using Python

Raj Chaudhary Can someone help me with the code, preferably using the Requests library to upload a file to a Google Cloud Storage signed URL in Python? Lundin Custer You can check out this sample code on Github . It shows a portable way to achieve this using P

Upload file to Google Cloud Storage signed URL using Python

Raj Chaudhary Can someone help me with the code, preferably using the Requests library to upload a file to a Google Cloud Storage signed URL in Python? Lundin Custer You can check out this sample code on Github . It shows a portable way to achieve this using P

upload file to google cloud storage with signed url using ajax

David I try to upload a file to my Google Cloud Storage using signed url. I have set CORS parameters on my bucket using gsutil Browser sends ajax request to Django backend to get signed url Django backend makes request to GCS to create signed url The signed ur

Upload file to Google Cloud Storage signed URL using Python

Raj Chaudhary Can someone help me with the code, preferably using the Requests library to upload a file to a Google Cloud Storage signed URL in Python? Lundin Custer You can check out this sample code on Github . It shows a portable way to achieve this using P

Upload file to Google Cloud Storage signed URL using Python

Raj Chaudhary Can someone help me with the code, preferably using the Requests library to upload a file to a Google Cloud Storage signed URL in Python? Lundin Custer You can check out this sample code on Github . It shows a portable way to achieve this using P

Create signed URL for Google Cloud Storage using NodeJS

Stuck Kennedy I'm trying to create a signature for a privately stored file in Google Cloud Storage; so I can distribute a limited time link. Currently doing this and the signature is too short...where am I going wrong? var crypto = require("crypto"); var ttl

Create signed URL for Google Cloud Storage using NodeJS

Stuck Kennedy I'm trying to create a signature for a privately stored file in Google Cloud Storage; so I can distribute a limited time link. Currently doing this and the signature is too short...where am I going wrong? var crypto = require("crypto"); var ttl