Upload a File to Google Cloud Storage using Python

In this tutorial, we will learn how to upload a local file as an object into a Cloud Storage bucket using Python. This is a foundational operation for any application that handles data files.

Python Code to Upload Object

We will use the upload_from_filename() method of the blob object.

Python

# Import required packages
from dotenv import load_dotenv
import os
from google.oauth2 import service_account
from google.cloud import storage

def main():
    # Load environment variables from a .env file
    load_dotenv()

    # Read values from environment variables
    credentials_path = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
    project_name = os.getenv("project_id")

    # Load service account credentials
    credentials = service_account.Credentials.from_service_account_file(
        credentials_path
    )

    # Create the Storage client
    client = storage.Client(credentials=credentials, project=project_name)

    # Define the bucket name and file details
    bucket_name = "new-bucket-via-python-sdk"
    destination_blob_name = "Code/python-sdk/requirements.txt"
    source_file_path = "requirements.txt"

    # Get the bucket
    bucket = client.bucket(bucket_name)

    # Create a blob (object reference) inside the bucket
    blob = bucket.blob(destination_blob_name)

    # Upload the local file to Cloud Storage
    blob.upload_from_filename(source_file_path)

    print(f"File uploaded to gs://{bucket_name}/{destination_blob_name}")

if __name__ == "__main__":
    main()    

Key Steps Explained

Note: The destination_blob_name can include slashes (for example, folder/file.txt) to simulate a folder structure inside the bucket.