Cloud Storage¶
Cloud Storage is a service for storing objects in Google Cloud Platform. The CloudStorage
class in gcp-pilot provides a high-level interface for interacting with Google Cloud Storage.
Installation¶
To use the Cloud Storage functionality, you need to install gcp-pilot with the storage extra:
Usage¶
Initialization¶
Initialize CloudStorage
from gcp_pilot.storage import CloudStorage
storage = CloudStorage() # (1)!
storage = CloudStorage(project_id="my-project") # (2)!
storage = CloudStorage(impersonate_account="service-account@project-id.iam.gserviceaccount.com") # (3)!
- Initialize with default credentials
- Initialize with specific project
- Initialize with service account impersonation
Creating a Bucket¶
Create a Cloud Storage Bucket
bucket = storage.create_bucket(name="my-bucket") # (1)!
bucket = storage.create_bucket( # (2)!
name="my-bucket",
region="us-central1",
project_id="my-project", # (3)!
exists_ok=True, # (4)!
)
- Create a bucket in the default region
- Create a bucket in a specific region
- Optional: defaults to the project associated with credentials
- Optional: if True, returns the existing bucket if it already exists
Uploading Files¶
The upload
method is versatile and can handle various types of input:
Upload Files to Cloud Storage
blob = storage.upload( # (1)!
source_file="/path/to/local/file.txt",
bucket_name="my-bucket",
target_file_name="file.txt", # (2)!
is_public=False, # (3)!
content_type="text/plain", # (4)!
)
blob = storage.upload( # (5)!
source_file="https://example.com/file.txt",
bucket_name="my-bucket",
target_file_name="file.txt",
)
blob = storage.upload( # (6)!
source_file="Hello, World!",
bucket_name="my-bucket",
target_file_name="hello.txt",
)
blob = storage.upload( # (7)!
source_file=b"Hello, World!",
bucket_name="my-bucket",
target_file_name="hello.txt",
)
with open("/path/to/local/file.txt", "rb") as f: # (8)!
blob = storage.upload(
source_file=f,
bucket_name="my-bucket",
target_file_name="file.txt",
)
- Upload a local file
- Optional: defaults to the source file name
- Optional: if True, makes the file publicly accessible
- Optional: sets the content type
- Upload from a URL
- Upload from a string
- Upload from bytes
- Upload from a file-like object
Getting a Bucket¶
Get a Cloud Storage Bucket
bucket = storage.get_bucket(name="my-bucket") # (1)!
bucket = storage.get_bucket( # (2)!
name="my-bucket",
auto_create_bucket=True,
region="us-central1", # (3)!
project_id="my-project", # (4)!
)
- Get a bucket
- Get a bucket, creating it if it doesn't exist
- Optional: used only if the bucket is created
- Optional: used only if the bucket is created
Copying Files¶
Copy Files in Cloud Storage
blob = storage.copy( # (1)!
source_file_name="file.txt",
source_bucket_name="source-bucket",
target_bucket_name="target-bucket",
target_file_name="file_copy.txt", # (2)!
project_id="my-project", # (3)!
region="us-central1", # (4)!
auto_create_bucket=False, # (5)!
)
- Copy a file from one bucket to another
- Optional: defaults to the source file name
- Optional: defaults to the project associated with credentials
- Optional: used only if a bucket is created
- Optional: if True, creates the target bucket if it doesn't exist
Moving Files¶
Move Files in Cloud Storage
blob = storage.move( # (1)!
source_file_name="file.txt",
source_bucket_name="source-bucket",
target_bucket_name="target-bucket",
target_file_name="file_moved.txt", # (2)!
project_id="my-project", # (3)!
region="us-central1", # (4)!
)
- Move a file from one bucket to another
- Optional: defaults to the source file name
- Optional: defaults to the project associated with credentials
- Optional: used only if a bucket is created
Deleting Files¶
Delete Files from Cloud Storage
storage.delete( # (1)!
file_name="file.txt",
bucket_name="my-bucket",
)
- Delete a file
Listing Files¶
List Files in Cloud Storage
for blob in storage.list_files(bucket_name="my-bucket"): # (1)!
print(f"File: {blob.name}")
for blob in storage.list_files(bucket_name="my-bucket", prefix="folder/"): # (2)!
print(f"File: {blob.name}")
- List all files in a bucket
- List files with a specific prefix
Getting a File¶
- Get a file by its GCS URI
Getting a Download URL¶
Get a Download URL for a File
# Generate a signed URL for downloading a file (1)!
url = storage.get_download_url(
bucket_name="my-bucket",
blob_name="file.txt",
expiration=timedelta(minutes=30), # (2)!
version="v4", # (3)!
)
- Generate a signed URL for downloading a file
- Optional: defaults to 5 minutes
- Optional: defaults to "v4"
Getting a GCS URI¶
- Get the GCS URI for a blob: "gs://my-bucket/file.txt
Error Handling¶
The CloudStorage class handles common errors and converts them to more specific exceptions: