A Python implementation for managing Google Cloud Storage buckets. This utility provides functions for creating, listing, and deleting buckets in Google Cloud Storage.
- Python 3.8.19
- Google Cloud Platform Account
- Project ID
- Configured Google Cloud credentials
Install the required Google Cloud libraries:
pip install google-cloud-storage
pip install google-auth google-auth-application-default
import os
import os.path
import sys
import venv
from google.cloud import storage
The script uses Application Default Credentials (ADC) for authentication:
def authenticate_implicit_with_adc(project_id="your-project-id"):
"""
Authenticates with Google Cloud using Application Default Credentials.
Args:
project_id (str): The project id of your Google Cloud project.
"""
storage_client = storage.Client(project=project_id)
buckets = storage_client.list_buckets()
Creates a new bucket in Google Cloud Storage with specified configurations.
def create_bucket_CloudStorage(bucket_name):
"""
Creates a new bucket in the Asia Southeast region with standard storage class.
Args:
bucket_name (str): Name for the new bucket
Returns:
google.cloud.storage.bucket.Bucket: The created bucket object
"""
Example usage:
new_bucket = create_bucket_CloudStorage('your-bucket-name')
Configuration details:
- Region: asia-southeast1
- Storage Class: STANDARD
Lists all buckets in your Google Cloud Storage project.
def list_buckets_CloudStorage():
"""
Lists all buckets in the project.
Handles exceptions and ensures proper client closure.
"""
Example usage:
list_buckets_CloudStorage()
Deletes a specified bucket from Google Cloud Storage.
def delete_bucket_CloudStorage(bucket_name):
"""
Deletes a specified bucket.
Args:
bucket_name (str): Name of the bucket to delete
"""
Example usage:
delete_bucket_CloudStorage("bucket-to-delete")
The implementation includes:
- Exception handling for bucket operations
- Proper client closure in finally blocks
- Error reporting for failed operations
project_id = "your-project-id"
storage_client = storage.Client(project=project_id)
-
Authentication:
- Use Application Default Credentials
- Keep credentials secure
- Never commit credentials to version control
-
Resource Management:
- Close storage client after use
- Handle exceptions appropriately
- Validate bucket names before operations
-
Storage Classes:
- Use appropriate storage class based on needs
- STANDARD is used by default
-
Regional Configuration:
- Default region is set to asia-southeast1
- Modify region based on your requirements
# Initialize
project_id = "your-project-id"
authenticate_implicit_with_adc(project_id)
storage_client = storage.Client(project=project_id)
# Create bucket
new_bucket = create_bucket_CloudStorage("new-bucket-name")
# List all buckets
list_buckets_CloudStorage()
# Delete bucket
delete_bucket_CloudStorage("bucket-to-delete")