Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

generate signedUrl for resource on Google Cloud Storage bucket

I would like to generate a signed url in Python, asynchronously. I have tried google.aio.storage, but it fails to authenticate.

What would be the steps to generate a signed url given Service Account Credentials, a bucket name and a filename on the bucket?

What does the request look like? 

Thank you!

0 4 2,412
4 REPLIES 4

Hi @roccofortuna98,

Welcome to Google Cloud Community!

To generate a signed URL for a resource on Google Cloud Storage bucket using Python, you can use the Google Cloud Storage client library for Python. Here are the steps you can follow:

1. Create a Google Cloud Storage client object:

from google.cloud import storage
storage_client = storage.Client()

2. Get a reference to the bucket and file you want to generate the signed URL for:

bucket_name = "my-bucket"
file_name = "path/to/my/file"
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(file_name)

3. Generate the signed URL with the generate_signed_url method:

url = blob.generate_signed_url(
expiration=300, # URL expires in 5 minutes
method="GET"
)

In the above example, the signed URL will be valid for 5 minutes (300 seconds) and allows GET requests. You can modify the expiration parameter to set the duration of the signed URL's validity.

The signed URL is returned as a string and can be used to access the file directly, without the need for authentication.

Note that you will need to have valid credentials with access to the bucket and file to generate a signed URL. The generate_signed_url method will use the credentials provided by the storage.Client() object to authenticate the request.

Thank you very much for the prompt response!

Unfortunately I am familiar with that solution, but I'm looking for an 

async

solution.

Is there an async variant of the function you provided above?

 

url = await blob.generate_signed_url(...)

 

Alternatively I found the provided script to sign offline, but an async solution would be the best.

Oh nevermind! 

Can you confirm there is no request to GCP required with this function? It's local signing, correct?

So it's simply a CPU bound process.

@christianpaula bumping. Would love to get confirmation on this. Thanks a lot!