Ejemplo n.º 1
0
def sign_url(blob: storage.Blob, *args, **kwargs):
    """cloudstorage signed url to download cloudstorage object without login
    Docs : https://cloud.google.com/storage/docs/access-control?hl=bg#Signed-URLs
    API : https://cloud.google.com/storage/docs/reference-methods?hl=bg#getobject
    """
    if config.DEBUG:
        return blob.generate_signed_url(*args, **kwargs)

    auth_request = Request()
    signing_credentials = compute_engine.IDTokenCredentials(
        auth_request,
        "",
        service_account_email=
        "*****@*****.**",
    )
    return blob.generate_signed_url(*args,
                                    **kwargs,
                                    credentials=signing_credentials,
                                    version="v4")
Ejemplo n.º 2
0
def generate_download_signed_url_v4(blob: storage.Blob, download_name: str):
    """Generates a v4 signed URL for downloading a blob.

    Note that this method requires a service account key file. You can not use
    this if you are using Application Default Credentials from Google Compute
    Engine or from the Google Cloud SDK.
    """
    url = blob.generate_signed_url(
        version="v4",
        expiration=datetime.timedelta(minutes=60),
        credentials=signing_credentials,
        response_disposition=f'attachment;filename={download_name}',
        response_type='application/pdf'
    )

    return url