Google Signed Url Expiry issue

Hello Everyone

I am facing the issue with google signed URL expiry time. I want to signed URL expire in 5 seconds but its not expiring. here is my python code:

Even I have tried with this code as well getting same issue

Thanks in advance

1 Like

Hello @testtechno ,Welcome on Google Cloud Community.

Do you fetch credentials somewhere in your code ? Check this one, I’ve modified a little bit https://cloud.google.com/storage/docs/access-control/signing-urls-with-helpers#download-object

import datetime
from google.oauth2 import service_account
from google.cloud import storage

# Path to the JSON key file
key_path = "CREDENTIALS_PATH_FOR_SERVICE_KEY.json"

# Create a credentials object from the key file
credentials = service_account.Credentials.from_service_account_file(key_path)

# Use the credentials to interact with Google Cloud services
client = storage.Client(credentials=credentials)

def generate_download_signed_url_v4(bucket_name, blob_name):
    """Generates a v4 signed URL for downloading a blob."""
    bucket = client.bucket(bucket_name)
    blob = bucket.blob(blob_name)

    url = blob.generate_signed_url(
        version="v4",
        expiration=datetime.timedelta(seconds=5),
        method="GET",
    )

    return url  # Return the URL without printing it

# Define your bucket and blob names
bucket_name = "BUCKET_NAME"  # Replace with your actual bucket name
blob_name = "YOUR_FILE"    # Replace with your actual blob (object) name

# Start the script and generate the signed URL
print("Script started")
url = generate_download_signed_url_v4(bucket_name, blob_name)
print(f"Generated URL: {url}")  # Print the URL here only

Remember to install libraries:
pip install google-auth
pip install google-cloud
pip install datetime


cheers,
DamianS
LinkedIn medium.com Cloudskillsboost

Thanks for reply, but same thing I am using but its not working. Here is my complete code example. Some time its working sometime not