Storage command-line tool

List available commands

The gsutil command can be used to perform a wide array of tasks. Run the help command to view a list of available commands:

!gsutil help

Create a storage bucket

Buckets are the basic containers that hold your data. Everything that you store in Cloud Storage must be contained in a bucket. You can use buckets to organize your data and control access to your data.

# Replace the string below with a unique name for the new bucket
bucket_name = "your-new-bucket"
!gsutil mb gs://{bucket_name}/

List buckets in a project

Replace ‘your-project-id’ in the cell below with your project ID and run the cell to list the storage buckets in your project.

# Replace the string below with your project ID
project_id = "your-project-id"
!gsutil ls -p $project_id
gs://your-new-bucket/

Get bucket metadata

The next cell shows how to get information on metadata of your Cloud Storage buckets.

!gsutil ls -L -b gs://{bucket_name}/
gs://your-new-bucket/ :
Storage class: MULTI_REGIONAL
Location constraint: US
...

Upload a local file to a bucket

Objects are the individual pieces of data that you store in Cloud Storage. Objects are referred to as “blobs” in the Python client library. There is no limit on the number of objects that you can create in a bucket.

!gsutil cp resources/us-states.txt gs://{bucket_name}/

List blobs in a bucket

!gsutil ls -r gs://{bucket_name}/**
gs://your-new-bucket/us-states.txt

Get a blob and display metadata

See Viewing and editing object metadata for more information about object metadata.

!gsutil ls -L  gs://{bucket_name}/us-states.txt
gs://your-new-bucket/us-states.txt:
Creation time: Fri, 08 Feb 2019 05:23:28 GMT
Update time: Fri, 08 Feb 2019 05:23:28 GMT
Storage class: STANDARD
Content-Language: en
Content-Length: 637
Content-Type: text/plain
...

Download a blob to a local directory

!gsutil cp gs://{bucket_name}/us-states.txt resources/downloaded-us-states.txt

Cleaning up

Delete a blob

!gsutil rm gs://{bucket_name}/us-states.txt

Delete a bucket

The following command deletes all objects in the bucket before deleting the bucket itself.

!gsutil rm -r gs://{bucket_name}/

Next Steps

Read more about Cloud Storage in the documentation:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store