Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new storage bucket in Google Cloud Storage.
GCP
from google.cloud import storage client = storage.Client() bucket = client.[1]('my-new-bucket')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using get_bucket instead of create_bucket will try to access an existing bucket.
list_buckets lists all buckets but does not create one.
delete_bucket removes a bucket, not creates it.
✗ Incorrect
The create_bucket method creates a new bucket in Google Cloud Storage.
2fill in blank
mediumComplete the code to upload a file to a bucket as an object.
GCP
bucket = client.get_bucket('my-existing-bucket') blob = bucket.blob('folder/myfile.txt') blob.[1]('localfile.txt')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using download_to_filename downloads the object instead of uploading.
delete removes the object, not uploads.
copy_to copies an object, not uploads a local file.
✗ Incorrect
The upload_from_filename method uploads a local file to the blob (object) in the bucket.
3fill in blank
hardFix the error in the code to list all objects in a bucket.
GCP
bucket = client.get_bucket('my-bucket') blobs = bucket.[1]() for blob in blobs: print(blob.name)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using get_blobs or get_objects causes errors because these methods do not exist.
list_objects is not a valid method in this client library.
✗ Incorrect
The correct method to list objects in a bucket is list_blobs.
4fill in blank
hardFill both blanks to delete an object from a bucket.
GCP
bucket = client.get_bucket('my-bucket') blob = bucket.[1]('my-object.txt') blob.[2]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'blob' instead of 'get_blob' will cause errors.
Using 'upload_from_filename' does not delete objects.
✗ Incorrect
Use get_blob to get the object, then delete to remove it.
5fill in blank
hardFill all three blanks to copy an object to another bucket.
GCP
source_bucket = client.get_bucket('source-bucket') source_blob = source_bucket.[1]('file.txt') destination_bucket = client.get_bucket('destination-bucket') destination_blob = destination_bucket.blob('file.txt') source_blob.[2](destination_blob) print('File copied to', [3].name)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'upload_from_filename' instead of 'copy_to' will not copy the object.
Printing the source blob's name instead of destination blob's name is incorrect.
✗ Incorrect
First get the source object with get_blob, then use copy_to to copy it to the destination blob. Finally, print the destination blob's name.