Google Cloud Storage OAuth 2.0 Boto Plugin

3.3 · deprecated · verified Tue Apr 14

This plugin enables the Boto library (version 2) to authenticate with Google Cloud Storage using OAuth 2.0 credentials, including service accounts and user accounts. It allows legacy Boto applications to interact with GCS. This library is deprecated; users are strongly encouraged to migrate to the official `google-cloud-storage` client library. The last release was 3.3 in March 2021, and the project is no longer actively maintained.

Warnings

Install

Imports

Quickstart

This example demonstrates how to establish a connection to Google Cloud Storage using the Boto library (v2) with `gcs-oauth2-boto-plugin`. It relies on standard Google Cloud authentication mechanisms like `gcloud auth application-default login` or `GOOGLE_APPLICATION_CREDENTIALS`. It then performs basic operations: connecting, checking/creating a bucket, uploading a file, and downloading its content. Replace 'your-gcs-boto-example-bucket' with an actual bucket name you have access to or create one.

import os
import gcs_oauth2_boto_plugin
from boto.gs.connection import GSConnection
from boto.exception import GSResponseError

# NOTE: This library is DEPRECATED. Use `google-cloud-storage` instead.
# This example assumes you have authenticated via:
# `gcloud auth application-default login`
# or have GOOGLE_APPLICATION_CREDENTIALS pointing to a service account key.

bucket_name = os.environ.get('GCS_BOTO_EXAMPLE_BUCKET', 'your-gcs-boto-example-bucket')

print(f"Attempting to connect to GCS using boto with gcs-oauth2-boto-plugin...")

try:
    # After importing gcs_oauth2_boto_plugin, boto will automatically use it.
    conn = GSConnection()

    # Check if the bucket exists or create it
    try:
        bucket = conn.get_bucket(bucket_name)
        print(f"Successfully connected to bucket '{bucket_name}'.")
    except GSResponseError as e:
        if e.status == 404:
            print(f"Bucket '{bucket_name}' not found, attempting to create...")
            bucket = conn.create_bucket(bucket_name)
            print(f"Bucket '{bucket_name}' created successfully.")
        else:
            raise

    # Upload a simple string to the bucket
    key_name = 'hello_boto_gcs.txt'
    key = bucket.new_key(key_name)
    key.set_contents_from_string('Hello from gcs-oauth2-boto-plugin!')
    print(f"Uploaded '{key_name}' to '{bucket_name}'.")

    # Download and print the content
    key_retrieved = bucket.get_key(key_name)
    if key_retrieved:
        content = key_retrieved.get_contents_as_string().decode('utf-8')
        print(f"Content of '{key_name}': {content}")
    else:
        print(f"Failed to retrieve '{key_name}'.")

    # Clean up (optional)
    # key_retrieved.delete()
    # print(f"Deleted '{key_name}'.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure you have authenticated to Google Cloud (e.g., `gcloud auth application-default login`) and have correct permissions.")

view raw JSON →