0
0
GCPcloud~5 mins

Creating buckets and uploading objects in GCP - Step-by-Step CLI Walkthrough

Choose your learning style9 modes available
Introduction
Sometimes you need a place in the cloud to store files like photos or documents. Creating a bucket is like making a folder in the cloud. Uploading objects means putting your files into that folder so you can use or share them later.
When you want to save backups of your important files safely in the cloud.
When you need to share files with your team without emailing large attachments.
When your app needs to store user-uploaded images or documents.
When you want to archive logs or data for later analysis.
When you want to organize files by project or purpose in separate cloud folders.
Config File - bucket-config.json
bucket-config.json
{
  "name": "example-bucket-12345",
  "location": "US",
  "storageClass": "STANDARD"
}

This JSON file defines the bucket's name, where it is located geographically, and the storage class which affects cost and access speed.

name: The unique name of your bucket.

location: The region where your bucket is stored.

storageClass: The type of storage, STANDARD is good for frequent access.

Commands
This command creates a new bucket named 'example-bucket-12345' in the US region with standard storage. We run this first to have a place to store files.
Terminal
gsutil mb -l US -c STANDARD gs://example-bucket-12345/
Expected OutputExpected
Creating gs://example-bucket-12345/...
-l US - Sets the bucket location to US region
-c STANDARD - Sets the storage class to STANDARD for frequent access
This command lists all buckets in your project to confirm the new bucket was created successfully.
Terminal
gsutil ls
Expected OutputExpected
gs://example-bucket-12345/
This command uploads the file 'sample-file.txt' from your computer to the bucket you created. This puts your file safely in the cloud.
Terminal
gsutil cp sample-file.txt gs://example-bucket-12345/
Expected OutputExpected
Copying file://sample-file.txt [Content-Type=text/plain]... / [1 files][ 12.0 B/ 12.0 B] 100% Done Operation completed over 1 objects/12.0 B.
This command lists the files inside your bucket to verify that your upload worked and the file is there.
Terminal
gsutil ls gs://example-bucket-12345/
Expected OutputExpected
gs://example-bucket-12345/sample-file.txt
Key Concept

If you remember nothing else from this pattern, remember: create a bucket first, then upload files into it to store them safely in the cloud.

Common Mistakes
Trying to upload a file before creating the bucket.
The upload fails because the destination bucket does not exist yet.
Always create the bucket first using 'gsutil mb' before uploading files.
Using a bucket name that is already taken by someone else.
Bucket names must be unique globally, so the command will fail if the name is used.
Choose a unique bucket name, often by adding numbers or your project name.
Not specifying the bucket location and storage class when creating the bucket.
This can lead to default settings that might not fit your needs and could cost more or be slower.
Use flags '-l' for location and '-c' for storage class to set these explicitly.
Summary
Use 'gsutil mb' to create a new bucket with a unique name, location, and storage class.
Use 'gsutil ls' to check your buckets and confirm creation.
Use 'gsutil cp' to upload files into your bucket.
Use 'gsutil ls gs://bucket-name/' to list files inside your bucket and verify uploads.