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

前端 未结 3 933
南方客
南方客 2021-01-05 12:54

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. :)

3条回答
  •  独厮守ぢ
    2021-01-05 13:28

    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.

提交回复
热议问题