import datetime from google.cloud import storage """Generates a v4 signed URL for uploading a blob using HTTP PUT. 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. """ # bucket_name = 'your-bucket-name' # blob_name = 'your-object-name' # storage_client = storage.Client() # cred = credentials.Certificate('servicekey.json') storage_client = storage.Client.from_service_account_json("servicekey.json"); bucket_name = 'farmbase-b2f7e.appspot.com' bucket = storage_client.bucket(bucket_name) blob = bucket.blob('DroneData') url = blob.generate_signed_url( version="v4", # This URL is valid for 15 minutes expiration=datetime.timedelta(minutes=1500), # Allow PUT requests using this URL. method="PUT", content_type="application/octet-stream", ) print("Generated PUT signed URL:") print(url) print("You can use this URL with any user agent, for example:") print( "curl -X PUT -H 'Content-Type: application/octet-stream' " "--upload-file my-file '{}'".format(url) ) print(url) #####different way using credentials to create the url###### # import os, google.auth # from google.auth.transport import requests # from google.auth import compute_engine # from datetime import datetime, timedelta # from google.cloud import storage # auth_request = requests.Request() # credentials, project = google.auth.default() # storage_client = storage.Client(project, credentials) # bucket_name = 'farmbase-b2f7e.appspot.com' # data_bucket = storage_client.bucket(bucket_name) # # data_bucket = storage_client.lookup_bucket(os.getenv("BUCKET_NAME")) # signed_blob_path = data_bucket.blob("DroneData") # expires_at_ms = datetime.now() + timedelta(minutes=1500) # # This next line is the trick! # signing_credentials = compute_engine.IDTokenCredentials(auth_request, "", service_account_email=credentials.service_account_email) # signed_url = signed_blob_path.generate_signed_url(expires_at_ms, credentials=signing_credentials, version="v4")