Google Cloud Storage (GCS) is a robust and highly scalable object storage service provided by Google Cloud. Developers can use it to store and retrieve any amount of data at any time. In this article, we will demonstrate how to connect to GCS using Python, create a new bucket, and perform common bucket operations.

Setting up the Environment

Before we can begin, ensure that you have the following prerequisites installed:

  1. Google Cloud SDK: This provides the gcloud command-line tool.
  2. Python and pip: We'll be using Python to interact with GCS.
  3. A Google Cloud Platform account and a project set up.

Creating a Service Account Key

To connect securely with Google Cloud from a Python application, we will need a service account key.

  1. Go to the Google Cloud Console.
  2. Navigate to IAM & Admin > Service Accounts.
  3. Click on “+ CREATE SERVICE ACCOUNT”.
  4. Provide a name and description for the service account.
  5. Grant appropriate permissions. For this tutorial, assign the role of 'Storage Admin'.
  6. Click on 'Create Key' and choose 'JSON'. This will download the key to your machine.
  7. Store this JSON file securely, as it provides access to your GCS resources.

Connecting to GCS Using Python

Once we have our service account key, we can leverage it to establish a connection to GCS from our Python script. The connection is instantiated using the google-cloud-storage library.

pip install google-cloud-storage

Now, let's write a Python script to connect to GCS and create a client.

from google.cloud import storage

# Create client using service account path
def create_gcs_client(service_account_key_path):
    return storage.Client.from_service_account_json(service_account_key_path)

client = create_gcs_client("path_to_your_service_account_key.json")


# Or you can set it to environment variables
set GOOGLE_APPLICATION_CREDENTIALS=PATH_TO_SERVICE_ACCOUNT_KEY

client = storage.Client()

Creating a Bucket

Google Cloud Storage organizes data into containers called "buckets". In this section, we create a new bucket to store our files.

def create_bucket(bucket_name):
    bucket = client.bucket(bucket_name)
    new_bucket = client.create_bucket(bucket)
    print(f"Bucket {bucket_name} created.")
    return new_bucket

create_bucket("your_unique_bucket_name")

Uploading Files to the Bucket

After having a bucket, we can store files in it. Here, we demonstrate how to upload a local file to our GCS bucket.

def upload_file(bucket_name, source_file_path, destination_blob_name):
    bucket = client.bucket(bucket_name)
    blob = bucket.blob(destination_blob_name)
    blob.upload_from_filename(source_file_path)
    print(f"File {source_file_path} uploaded to {destination_blob_name}.")

upload_file("your_bucket_name", "local_path_to_file.txt", "destination_path_in_bucket.txt")

Downloading Files from the Bucket

Sometimes, we might need to retrieve files from our bucket. This code shows how to download a file from GCS to our local machine.

def download_file(bucket_name, source_blob_name, destination_file_path):
    bucket = client.bucket(bucket_name)
    blob = bucket.blob(source_blob_name)
    blob.download_to_filename(destination_file_path)
    print(f"File {source_blob_name} downloaded to {destination_file_path}.")

download_file("your_bucket_name", "source_path_in_bucket.txt", "local_path_to_save_file.txt")

Deleting Files from the Bucket

Over time, certain files may become obsolete. This section covers how to remove a file from our GCS bucket.

def delete_blob(bucket_name, blob_name):
    bucket = client.bucket(bucket_name)
    blob = bucket.blob(blob_name)
    blob.delete()
    print(f"Blob {blob_name} deleted from {bucket_name}.")

delete_blob("your_bucket_name", "path_in_bucket_to_file_to_delete.txt")

With these concise explanations and the associated functions, interacting with Google Cloud Storage using Python becomes straightforward. Always prioritize security by safeguarding your service account keys and maintaining appropriate permissions.