Cloud Storage client library

Create a storage bucket

from import storage
client = storage.Client()
print("Client created using default project: {}".format(client.project))
# client = storage.Client(project='your-project-id')
# Replace the string below with a unique name for the new bucket
bucket_name = "your-new-bucket"

# Creates the new bucket
bucket = client.create_bucket(bucket_name)

print("Bucket {} created.".format(

List buckets in a project

buckets = client.list_buckets()

print("Buckets in {}:".format(client.project))
for item in buckets:
print("\t" +

Get bucket metadata

bucket = client.get_bucket(bucket_name)

print("Bucket name: {}".format(
print("Bucket location: {}".format(bucket.location))
print("Bucket storage class: {}".format(bucket.storage_class))

Upload a local file to a bucket

blob_name = "us-states.txt"
blob = bucket.blob(blob_name)

source_file_name = "resources/us-states.txt"

print("File uploaded to {}.".format(

List blobs in a bucket

blobs = bucket.list_blobs()

print("Blobs in {}:".format(
for item in blobs:
print("\t" +

Get a blob and display metadata

blob = bucket.get_blob(blob_name)

print("Name: {}".format(
print("Size: {} bytes".format(blob.size))
print("Content type: {}".format(blob.content_type))
print("Public URL: {}".format(blob.public_url))

Download a blob to a local directory

output_file_name = "resources/downloaded-us-states.txt"

print("Downloaded blob {} to {}.".format(, output_file_name))

Cleaning up

Delete a blob

blob = client.get_bucket(bucket_name).get_blob(blob_name)

print("Blob {} deleted.".format(

Delete a bucket

bucket = client.get_bucket(bucket_name)

print("Bucket {} deleted.".format(

Next Steps




Machine Learning Scientist

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Android fundamentals 04.4: User navigation

Networking Basics

What does data engineering entail?

My Life Before Flatiron School

I’m Beginning to Wonder if It Was Worth It

Monitoring apps on kubernetes from Prometheus running outside kubernetes cluster

Hello Halagame

🔰How to Write an Ansible Play-Book To: 🔹 Configure Docker 🔹Setting up a Webserver

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
Mohamed Niang

Mohamed Niang

Machine Learning Scientist

More from Medium

Temperature sensing for Jupiter Garage

On-Premise vs. Cloud: Key Differences, Pros, and Cons

ElastAlert 2 for OpenShift 4.9

Finding nearest neighbour with Cloud Firestore.