Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create Google Cloud Storage Signed Urls on App Engine Python

I couldn't find a simple example on how to implement Google Cloud Storage Signed Urls on Google App Engine with Python. Please write a step by step guide. :)

like image 440
Peter Korinek TellusTalk Avatar asked Jun 15 '15 10:06

Peter Korinek TellusTalk


People also ask

How do I create a GCP signed URL?

You may also specify RESUMABLE to create a signed resumable upload start URL. When using a signed URL to start a resumable upload session, you will need to specify the 'x-goog-resumable:start' header in the request or else signature validation will fail. Specify the private key password instead of prompting.

How do I create a signed URL?

To create a valid pre-signed URL for your object, you must provide your security credentials, specify a bucket name, an object key, specify the HTTP method (for instance the method is "GET" to download the object) and expiration date and time. Anyone who receives the pre-signed URL can then access the object.

How do I use Google Cloud Storage URI?

To load data from a Cloud Storage data source, you must provide the Cloud Storage URI. The Cloud Storage URI comprises your bucket name and your object (filename). For example, if the Cloud Storage bucket is named mybucket and the data file is named myfile. csv , the bucket URI would be gs://mybucket/myfile.csv .


2 Answers

I created this repo: https://github.com/voscausa/appengine-gcs-signed-url

Using GAE Pyrthon app_identity.get_service_account_name() and app_identity.sign_blob() makes creating signed url's very easy, without using a PEM key. The app shows how to download a GCS file.

But if you use the SDK to test the app, you have to use:

  1. --appidentity_email_address
  2. --appidentity_private_key_path

because creating a signed url is not part of the GCS client.

like image 63
voscausa Avatar answered Oct 28 '22 06:10

voscausa


The other solutions work but there is a simpler way using generate_signed_url. This method does the same thing as @voscausa's answer but is less tedious and has custom exceptions and support for other environments.

def sign_url(obj, expires_after_seconds=60):

    client = storage.Client()
    default_bucket = '%s.appspot.com' % app_identity.get_application_id()
    bucket = client.get_bucket(default_bucket)
    blob = storage.Blob(obj, bucket)

    expiration_time = int(time.time() + expires_after_seconds)

    url = blob.generate_signed_url(expiration_time)

    return url

What vascausa said regarding local development server testing

But if you use the SDK to test the app, you have to use:

--appidentity_email_address

--appidentity_private_key_path

because creating a signed url is not part of the GCS client.

still holds.

like image 39
Alex Avatar answered Oct 28 '22 06:10

Alex