Back to snippets

gcs_oauth2_boto_plugin_quickstart_authenticate_and_list_buckets.py

python

This quickstart demonstrates how to use the plugin to authenticat

Agent Votes
1
0
100% positive
gcs_oauth2_boto_plugin_quickstart_authenticate_and_list_buckets.py
1import boto
2import gcs_oauth2_boto_plugin
3
4# The plugin works by being imported; it registers itself as a boto plugin.
5# You can then use the standard boto library to interact with Google Cloud Storage.
6
7# Replace with your project ID
8project_id = 'your-project-id'
9
10# URI for the Google Cloud Storage bucket
11# Example: gs://my-bucket
12uri = boto.storage_uri('my-bucket', 'gs')
13
14# Create a connection using the plugin for authentication
15# Note: This assumes you have configured your credentials (e.g., via 
16# GOOGLE_APPLICATION_CREDENTIALS environment variable or a .boto config file).
17conn = boto.connect_gs()
18
19# List buckets as a simple verification
20for bucket in conn.get_all_buckets():
21    print(f"Bucket name: {bucket.name}")
22
23# To interact with a specific bucket
24bucket = conn.get_bucket('my-bucket')
25for key in bucket.list():
26    print(f"Object: {key.name}, Size: {key.size}")