0
0
GCPcloud~10 mins

Signed URLs for temporary access in GCP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a signed URL for a Google Cloud Storage object.

GCP
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
Drag options to blanks, or click blank then click option'
Adatetime.timedelta(hours=1)
B3600
C60
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an integer instead of a timedelta object.
Setting expiration to None which causes errors.
2fill in blank
medium

Complete the code to specify the HTTP method allowed for the signed URL.

GCP
url = blob.generate_signed_url(expiration=datetime.timedelta(hours=1), method=[1])
Drag options to blanks, or click blank then click option'
A"POST"
B"GET"
C"PUT"
D"DELETE"
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST or PUT when only reading is intended.
Omitting the method parameter.
3fill in blank
hard

Fix the error in the code to correctly generate a signed URL with service account credentials.

GCP
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))
Drag options to blanks, or click blank then click option'
Acredentials
Bservice_account
Cstorage
Dclient
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the module name instead of the credentials object.
Passing the client object itself.
4fill in blank
hard

Fill both blanks to generate a signed URL that allows uploading a file with PUT method and sets content type.

GCP
url = blob.generate_signed_url(expiration=[1], method=[2], content_type='text/plain')
Drag options to blanks, or click blank then click option'
Adatetime.timedelta(minutes=30)
B"PUT"
C"GET"
Ddatetime.timedelta(hours=2)
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET method when upload is intended.
Setting expiration too long or too short without reason.
5fill in blank
hard

Fill all three blanks to generate a signed URL with custom headers, expiration, and HTTP method.

GCP
url = blob.generate_signed_url(expiration=[1], method=[2], headers=[3])
Drag options to blanks, or click blank then click option'
Adatetime.timedelta(minutes=10)
B"GET"
C{'x-goog-content-length-range': '0,1048576'}
D"POST"
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST method when GET is intended.
Omitting headers or setting invalid expiration.