Create Google Cloud Storage Bucket Using CLI In Google Cloud Platform

you can refer on how to Create Google Cloud Storage Bucket Using UI/console In Google Cloud Platform here

No alt text provided for this image

Using gsutil – command line (Cloud Shell)  Click on above icon and you can see shell similar to this opening up.

No alt text provided for this image

Creating Google bucket using CLI:

To set your Cloud Platform project in this session use “gcloud config set project [PROJECT_ID]”

·       gcloud config set project bigquery-demo1-326311

·       gsutil –help

·       To list all in console just use “ls”.

·       Create a bucket

o  gsutil mb gs://demo02-bucket

If bucket name already exists we will get error code 409. 409 A Cloud Storage bucket named ‘demo02-bucket’ already exists. Try another name. Bucket names must be globally unique across all Google Cloud projects, including those outside of your organization.

Upload file from local to GCP cloud storage:

No alt text provided for this image

·       Click on Upload button and you can choose file to upload into GCP. Here I selected health.json and patient.json file into Cloud storage from cloudshell.

No alt text provided for this image
No alt text provided for this image

The same how it looks in GCP UI.

No alt text provided for this image

·       Then to load the uploaded file into bucket created, follow below commands. To give global access to a file in a bucket

o   gsutil acl ch -u ALLUsers:R gs://demo02-bucket/patient.json

No alt text provided for this image

·       Copy data from one bucket to another bucket. Error: AccessDeniedException: 403 Object ‘demo01-bucket/health.json’ is subject to bucket’s retention policy and cannot be deleted, overwritten or archived until 2021-09-30T02:01:14.331731-07:00

o  gsutil cp gs://demo01-bucket/* gs://demo02-bucket

No alt text provided for this image

NOTE: If retention policy is present in bucket, you are likely to face this error “Error: 403, subject to bucket’s retention policy and cannot be deleted, overwritten or archived until 2021-09-30T02:01:14.33”

·       How to enable version in gsutil

o  gsutil versioning get gs://demo01-bucket

No alt text provided for this image

NOTE: response: gs://demo01-bucket: Suspended (BadRequestException: 400 Bucket’s Versioning cannot be set for Bucket ‘demo01-bucket’ that has retention policy.)

·       Enabling the versioning in demo01-bucket

o  gsutil versioning set on gs://demo01-bucket

No alt text provided for this image

·       To list all the files in the bucket

o  gsutil ls gs://demo02-bucket

No alt text provided for this image

·       To list all hidden files in the bucket

o  gsutil ls -a gs://demo02-bucket

No alt text provided for this image

·       After enabling version in gs://demo02-bucket, Try running the command

o  gsutil cp gs://demo01-bucket/* gs://demo02-bucket

No alt text provided for this image

·       To list all the files in the bucket

o  gsutil ls -a gs://demo02-bucket

No alt text provided for this image
                                                                          Happy Learning!
Please connect me on below
Linkedin Profile: https://www.linkedin.com/in/vignesh-sekar-sujatha-02aa9b125/
Github Profile: https://github.com/VigneshSs-07

Leave a Comment

Your email address will not be published. Required fields are marked *