Complete the code to create a signed URL for a Google Cloud Storage object.
from google.cloud import storage import datetime def generate_signed_url(bucket_name, blob_name): client = storage.Client() bucket = client.bucket(bucket_name) blob = bucket.blob(blob_name) url = blob.generate_signed_url(expiration=[1]) return url
The expiration parameter requires a datetime.timedelta object to specify how long the signed URL is valid.
Complete the code to specify the HTTP method allowed for the signed URL.
url = blob.generate_signed_url(expiration=datetime.timedelta(hours=1), method=[1])
POST or PUT when only reading is intended.Signed URLs commonly allow GET method to download objects.
Fix the error in the code to correctly generate a signed URL with service account credentials.
from google.oauth2 import service_account import datetime from google.cloud import storage credentials = service_account.Credentials.from_service_account_file('key.json') client = storage.Client(credentials=[1]) bucket = client.bucket('my-bucket') blob = bucket.blob('file.txt') signed_url = blob.generate_signed_url(expiration=datetime.timedelta(minutes=15))
The Client constructor expects the credentials object passed as the credentials parameter.
Fill both blanks to generate a signed URL that allows uploading a file with PUT method and sets content type.
url = blob.generate_signed_url(expiration=[1], method=[2], content_type='text/plain')
Use PUT method to allow uploading, and set expiration to 30 minutes.
Fill all three blanks to generate a signed URL with custom headers, expiration, and HTTP method.
url = blob.generate_signed_url(expiration=[1], method=[2], headers=[3])
Set expiration to 10 minutes, allow GET method, and restrict content length with headers.