Last modified: Sep 16, 2026

Google Cloud Storage Python Tutorial

Getting Started with Google Cloud Storage in Python

Google Cloud Storage (GCS) is a scalable object storage service. It allows you to store and retrieve large amounts of data. In Python, you interact with GCS using the google-cloud-storage library.

This tutorial covers the basics. You will learn how to create a client. You will also learn how to upload and download files. We will explain how to manage buckets and objects.

Installing the Library

Start by installing the required package. Use pip to install google-cloud-storage.


pip install google-cloud-storage

Setting Up Authentication

To access GCS, you need credentials. Create a service account key in the Google Cloud Console. Download the JSON file. Then set the environment variable.


export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/service-account-file.json"

Creating a Storage Client

Use the Client class to connect to GCS. This client handles all requests.


from google.cloud import storage

# Initialize a client
client = storage.Client()
print("Client created successfully")

Client created successfully

Listing Buckets

You can list all buckets in your project using the list_buckets method.


buckets = list(client.list_buckets())
for bucket in buckets:
    print(bucket.name)

my-bucket-123
another-bucket-456

Uploading Files to a Bucket

Use the blob.upload_from_filename method to upload a local file.


bucket = client.bucket("my-bucket-123")
blob = bucket.blob("hello.txt")
blob.upload_from_filename("hello.txt")
print("File uploaded successfully")

File uploaded successfully

Downloading Files from a Bucket

Use the blob.download_to_filename method to download files.


blob.download_to_filename("downloaded_hello.txt")
print("File downloaded successfully")

File downloaded successfully

Managing Object Metadata

You can retrieve metadata using the blob property. Update it with patch.


blob.reload()  # Fetch latest metadata
print(f"Content Type: {blob.content_type}")
print(f"Size: {blob.size} bytes")

Content Type: text/plain
Size: 12 bytes

Deleting Objects and Buckets

Use the blob.delete method to remove an object. Use bucket.delete for buckets.


blob.delete()
print("Object deleted")

Object deleted

Working with Multiple Files

For batch operations, iterate through files. Use upload_from_file for streams.


import os

bucket = client.bucket("my-bucket-123")
for filename in os.listdir("local_folder"):
    blob = bucket.blob(filename)
    blob.upload_from_filename(os.path.join("local_folder", filename))
    print(f"Uploaded {filename}")

Uploaded file1.txt
Uploaded file2.txt

Error Handling Best Practices

Wrap operations in try-except blocks. Catch GoogleCloudError exceptions.


from google.cloud.exceptions import GoogleCloudError

try:
    blob.upload_from_filename("hello.txt")
    print("Upload successful")
except GoogleCloudError as e:
    print(f"Upload failed: {e}")

Upload successful

Conclusion

Google Cloud Storage integrates seamlessly with Python. The google-cloud-storage library provides powerful methods. You can upload, download, and manage files easily. Always handle errors gracefully. Secure your credentials properly. For advanced use cases, explore signed URLs and lifecycle rules. Check out our Python Google Drive API Guide for more cloud storage tutorials.