Skip to main content

GCP Cloud Storage Buckets: Manage Buckets via CLI

574 words·
GCP Google Cloud SDK Object Storage
Table of Contents

Prerequisites
#

Google Cloud SDK
#

Installation
#

The Google Cloud SDK (Software Development Kit) includes the gcloud command-line interface (CLI).

# Install dependencies
sudo apt install curl apt-transport-https ca-certificates gnupg -y

# Add package source
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list

# Import the Google Cloud public key
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /usr/share/keyrings/cloud.google.gpg

# Update package index
sudo apt update

# Install the Google Cloud SDK
sudo apt install google-cloud-sdk -y
# Verify installation / list version
gcloud version

# Shell output:
Google Cloud SDK 506.0.0
alpha 2025.01.10
beta 2025.01.10
bq 2.1.11
bundled-python3-unix 3.11.9
core 2025.01.10
gcloud-crc32c 1.0.0
gsutil 5.33

Login to GCP / Verify Login
#

Open the link from the shell in a browser, login to GCP and paste the verification code in the shell.

# Alternative use the following command to login without a browser
gcloud auth login --no-launch-browser

Verify Login:

# List Google Cloud accounts that are currently authenticated
gcloud auth list

Create new Project
#

# Create a new GCP project: Syntax
gcloud projects create [PROJECT_ID] --name "[PROJECT_NAME]"

# Create a new GCP project: Example
gcloud projects create storage-example-1 --name="Storage Example" --labels=type=storage

Set Default Project
#

# Set the default project: Syntax
gcloud config set project <PROJECT_ID>


# Set the default project: Example
gcloud config set project storage-example-1

# Shell output:
Updated property [core/project].

Enable Project APIs: Cloud Storage API
#

# Enable the necessary APIs, that are used in the project
gcloud services enable storage.googleapis.com

# Shell output:
Operation "operations/acf.p2-249181329052-8b52d534-2fc8-4221-88b8-7f17b4df9d95" finished successfully.



Storage Bucket
#

Create Bucket
#

Storage Classes: https://cloud.google.com/storage/docs/storage-classes

# Create Bucket
gcloud storage buckets create gs://jk-example-bucket \
  --default-storage-class STANDARD \
  --location us-central1 \
  --uniform-bucket-level-access
  • --uniform-bucket-level-access All permissions are applied to the entire bucket uniformly, rather than allowing fine-grained access control at the individual object level.

Location Options: Single Region, Dual-region, Multi-region


Verify Bucket
#

# List Buckets in curent project
gcloud storage buckets list

# Shell output:
---
creation_time: 2025-01-25T10:41:40+0000
default_storage_class: STANDARD
generation: 1737801700534338114
location: US-CENTRAL1
location_type: region
metageneration: 1
name: jk-example-bucket
public_access_prevention: inherited
soft_delete_policy:
  effectiveTime: '2025-01-25T10:41:40.732000+00:00'
  retentionDurationSeconds: '604800'
storage_url: gs://jk-example-bucket/
uniform_bucket_level_access: true
update_time: 2025-01-25T10:41:40+0000

CLI Commands
#

Upload, Download, Delete & List
#

# List commands / help
gcloud storage cp --help
# Upload file to Bucket
gcloud storage cp some-file.txt gs://jk-example-bucket

# Upload multiple files to Bucket
gcloud storage cp file1.txt file2.txt gs://jk-example-bucket

# Upload folder to Bucket
gcloud storage cp -r /tmp/example-folder/ gs://jk-example-bucket
# Download file from Bucket
gcloud storage cp gs://jk-example-bucket/some-file.txt /tmp

# Download folder from Bucket
gcloud storage cp -r gs://jk-example-bucket/example-folder .
# Delete file from Bucket
gcloud storage rm gs://jk-example-bucket/some-file.txt

# Delete folder from Bucket
gcloud storage rm -r gs://jk-example-bucket/example-folder

# Delete all files from Bucket
gcloud storage rm -r gs://jk-example-bucket/*
# List files in Bucket
gcloud storage ls gs://jk-example-bucket

# List files in Bucket: Details
gcloud storage ls -l gs://jk-example-bucket

Rsync
#

# Sync local directory to Bucket: Synchronize contents of "example-folder"
gsutil rsync -r ./example-folder gs://jk-example-bucket

# Sync local directory to Bucket: Synchronize the "example-folder" itself
gsutil rsync -r ./example-folder gs://jk-example-bucket/example-folder
# Sync files from Bucket
gsutil rsync -r gs://jk-example-bucket /tmp/example-folder



Delete Bucket
#

# Delete all files in the Bucket
gcloud storage rm -r gs://jk-example-bucket
# Delete the Bucket
gcloud storage buckets delete gs://jk-example-bucket
# List Buckets / Verify the Bucket was deleted
gcloud storage buckets list

# Shell output:
Listed 0 items.



Links #

## GCP Documentation: Create Bucket
https://cloud.google.com/storage/docs/creating-buckets#command-line

## GCP Documentation: Storage Classes
https://cloud.google.com/storage/docs/storage-classes