Bird
Raised Fist0
GCPcloud~10 mins

Creating buckets and uploading objects in GCP - Interactive Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new Cloud Storage bucket named 'my-bucket'.

GCP
from google.cloud import storage

client = storage.Client()
bucket = client.[1]('my-bucket')
Drag options to blanks, or click blank then click option'
Acreate_bucket
Bget_bucket
Clist_buckets
Ddelete_bucket
Attempts:
3 left
💡 Hint
Common Mistakes
Using get_bucket instead of create_bucket will try to access an existing bucket.
list_buckets returns all buckets, not create one.
delete_bucket removes a bucket, not create it.
2fill in blank
medium

Complete the code to upload a file 'photo.jpg' to the bucket under the name 'images/photo.jpg'.

GCP
bucket = client.get_bucket('my-bucket')
blob = bucket.blob('[1]')
blob.upload_from_filename('photo.jpg')
Drag options to blanks, or click blank then click option'
Aimages/photo.jpg
Bphoto
Cimages_photo.jpg
Dphoto.jpg
Attempts:
3 left
💡 Hint
Common Mistakes
Using just 'photo.jpg' uploads to the root, not inside 'images' folder.
Using 'photo' misses the file extension.
Using 'images_photo.jpg' is not a valid path with folders.
3fill in blank
hard

Fix the error in the code to download the file 'images/photo.jpg' from the bucket to local file 'downloaded.jpg'.

GCP
bucket = client.get_bucket('my-bucket')
blob = bucket.blob('images/photo.jpg')
blob.[1]('downloaded.jpg')
Drag options to blanks, or click blank then click option'
Adownload_as_string
Bdownload_to_file
Cdownload_to_filename
Dupload_from_filename
Attempts:
3 left
💡 Hint
Common Mistakes
Using upload_from_filename uploads a file instead of downloading.
download_as_string returns bytes, not saving to file.
download_to_file requires a file object, not a filename string.
4fill in blank
hard

Fill both blanks to list all bucket names in the project.

GCP
buckets = client.[1]()
for bucket in buckets:
    print(bucket.[2])
Drag options to blanks, or click blank then click option'
Alist_buckets
Bname
Cid
Dget_bucket
Attempts:
3 left
💡 Hint
Common Mistakes
Using get_bucket tries to get one bucket, not list all.
Using 'id' prints the bucket's id, not the name.
Using 'list_bucket' (missing s) causes error.
5fill in blank
hard

Fill all three blanks to create a bucket, upload a file, and print the public URL.

GCP
bucket = client.[1]('my-new-bucket')
blob = bucket.blob('[2]')
blob.upload_from_filename('document.pdf')
print(blob.[3])
Drag options to blanks, or click blank then click option'
Acreate_bucket
Bdocument.pdf
Cpublic_url
Dget_bucket
Attempts:
3 left
💡 Hint
Common Mistakes
Using get_bucket instead of create_bucket won't create a new bucket.
Using a wrong blob name causes upload errors.
Trying to print blob.name instead of public_url won't show the URL.

Practice

(1/5)
1. What is the main purpose of creating a bucket in Google Cloud Storage?
easy
A. To manage user permissions
B. To organize and store files in the cloud
C. To create databases
D. To run virtual machines

Solution

  1. Step 1: Understand what a bucket is

    A bucket is a container in cloud storage used to hold files or objects.
  2. Step 2: Identify the purpose of buckets

    Buckets help organize and store files safely in the cloud for easy access and management.
  3. Final Answer:

    To organize and store files in the cloud -> Option B
  4. Quick Check:

    Bucket = Storage container [OK]
Hint: Buckets hold files in cloud storage, not run machines [OK]
Common Mistakes:
  • Confusing buckets with virtual machines
  • Thinking buckets create databases
  • Mixing buckets with user permission tools
2. Which command correctly creates a new bucket named my-bucket in Google Cloud Storage using the gcloud tool?
easy
A. gcloud storage buckets create my-bucket
B. gcloud create bucket my-bucket
C. gsutil mb gs://my-bucket
D. gcloud storage create-bucket my-bucket

Solution

  1. Step 1: Recall the gcloud syntax for bucket creation

    The correct syntax is gcloud storage buckets create [BUCKET_NAME].
  2. Step 2: Match the command with the correct syntax

    gcloud storage buckets create my-bucket matches the correct syntax exactly.
  3. Final Answer:

    gcloud storage buckets create my-bucket -> Option A
  4. Quick Check:

    gcloud storage buckets create = create bucket [OK]
Hint: Use 'gcloud storage buckets create' to make buckets [OK]
Common Mistakes:
  • Using 'gcloud create bucket' which is invalid syntax
  • Confusing gsutil command with gcloud syntax
  • Adding hyphens incorrectly in command
3. What will be the result of running this command?
gsutil cp file.txt gs://example-bucket/
medium
A. Downloads file.txt from example-bucket
B. Deletes file.txt from local storage
C. Creates a new bucket named example-bucket
D. Uploads file.txt to the example-bucket in Google Cloud Storage

Solution

  1. Step 1: Understand the gsutil cp command

    The gsutil cp command copies files between local and cloud storage.
  2. Step 2: Analyze the command arguments

    It copies file.txt from local to the bucket gs://example-bucket/, uploading the file.
  3. Final Answer:

    Uploads file.txt to the example-bucket in Google Cloud Storage -> Option D
  4. Quick Check:

    gsutil cp local-to-bucket = upload [OK]
Hint: gsutil cp localfile gs://bucket = upload file [OK]
Common Mistakes:
  • Thinking cp deletes local files
  • Assuming it creates buckets automatically
  • Confusing upload with download direction
4. You tried to upload a file using gsutil cp myfile.txt gs://my-bucket/ but got an error saying the bucket does not exist. What is the most likely fix?
medium
A. Run the command with sudo
B. Rename the file to match the bucket name
C. Create the bucket first using gsutil mb gs://my-bucket
D. Delete the file and try again

Solution

  1. Step 1: Understand the error cause

    The error means the bucket my-bucket does not exist in cloud storage.
  2. Step 2: Fix by creating the bucket

    You must create the bucket first using gsutil mb gs://my-bucket before uploading files.
  3. Final Answer:

    Create the bucket first using gsutil mb gs://my-bucket -> Option C
  4. Quick Check:

    Bucket missing? Create it first [OK]
Hint: Create bucket before upload to avoid errors [OK]
Common Mistakes:
  • Trying to upload without bucket creation
  • Changing file name instead of bucket
  • Using sudo unnecessarily
5. You want to create a bucket named data-archive in the us-central1 region with versioning enabled to keep old versions of files. Which sequence of commands achieves this?
hard
A. gsutil mb -l us-central1 gs://data-archive && gsutil versioning set on gs://data-archive
B. gcloud storage buckets create data-archive --location=us-central1 && gcloud storage buckets update data-archive --versioning-enabled
C. gcloud storage buckets create data-archive --region=us-central1 && gcloud storage buckets enable-versioning data-archive
D. gsutil create bucket data-archive us-central1 && gsutil enable versioning gs://data-archive

Solution

  1. Step 1: Create bucket with location using gsutil

    The command gsutil mb -l us-central1 gs://data-archive creates the bucket in the correct region.
  2. Step 2: Enable versioning on the bucket

    The command gsutil versioning set on gs://data-archive turns on versioning to keep old file versions.
  3. Final Answer:

    gsutil mb -l us-central1 gs://data-archive && gsutil versioning set on gs://data-archive -> Option A
  4. Quick Check:

    Create bucket + enable versioning with gsutil [OK]
Hint: Use gsutil mb and gsutil versioning set on for versioned buckets [OK]
Common Mistakes:
  • Using wrong flags like --region instead of -l
  • Trying to enable versioning with wrong commands
  • Mixing gcloud and gsutil commands incorrectly