Skip to content

Commit 5ed2f59

Browse files
committed
moved class GoogleStorageClient to bucket.py
1 parent e8844b3 commit 5ed2f59

File tree

4 files changed

+110
-3
lines changed

4 files changed

+110
-3
lines changed

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from dotenv import load_dotenv
1616
from typing import Dict, List
1717
from src.evals import Evals, ResponseEvals
18-
from src.gcp import GoogleStorageClient
18+
from src.bucket import GoogleStorageClient
1919
from src.logger import Logger
2020
from src.models import (
2121
ClaudeAPIClient,

src/bucket.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"""
2+
Client script to cloud storage bucket (Azure, AWS, GCP)
3+
4+
Author: Abhishek Sriram <noobsiecoder@gmail.com>
5+
Date: Aug 22nd, 2025
6+
Place: Boston, MA
7+
"""
8+
9+
import os
10+
from src.logger import Logger
11+
from google.cloud import storage
12+
from google.oauth2 import service_account
13+
14+
15+
# TODO: Handle storage in Azure Storage
16+
class AzureStorageClient:
17+
"""
18+
Handles all operations in Azure Storage bucket
19+
"""
20+
21+
22+
# TODO: Handle storage in AWS S3 bucket
23+
class AWSStorageClient:
24+
"""
25+
Handles all operations in AWS S3 bucket
26+
"""
27+
28+
29+
class GoogleStorageClient:
30+
"""
31+
Handles all operations in GCP Storage bucket
32+
"""
33+
34+
def __init__(self):
35+
"""
36+
Loads when object is instantiated
37+
"""
38+
self.client = None
39+
self.log = Logger(
40+
"gcp_storage"
41+
).get_logger() # Initialize logger instance for tracking GCP storage operations
42+
43+
def connect(self) -> bool:
44+
"""
45+
Establish connection
46+
47+
Returns:
48+
--------
49+
Bool value to confirm connection
50+
"""
51+
service_filepath = "secrets/gcp-storage.json"
52+
try:
53+
if not os.path.exists(service_filepath):
54+
raise FileNotFoundError(f"File not found in path: {service_filepath}")
55+
56+
# Initialize GCS client with service account credentials
57+
# The credentials file should contain the service account key in JSON format
58+
credentials = service_account.Credentials.from_service_account_file(
59+
service_filepath
60+
)
61+
# Create a storage client using the loaded credentials
62+
self.client = storage.Client(credentials=credentials)
63+
return True
64+
except Exception as err:
65+
self.log.error(
66+
f"Error while trying to read secret file for GCP storage: {err}"
67+
)
68+
return False
69+
70+
def upload_file(self, local_file_path, bucket_name, blob_name=None):
71+
"""
72+
Upload a file to Google Cloud Storage
73+
74+
Parameters:
75+
-----------
76+
local_file_path : str
77+
Path to the local file that needs to be uploaded
78+
bucket_name : str
79+
Name of the GCS bucket where the file will be uploaded
80+
blob_name : str, optional
81+
Custom name for the file in GCS. If None, uses the original filename
82+
83+
Returns:
84+
--------
85+
None
86+
"""
87+
# Get a reference to the target bucket
88+
# Note: This doesn't verify if the bucket exists
89+
bucket = self.client.bucket(bucket_name)
90+
91+
# If no custom blob name is provided, extract the filename from the local path
92+
# For example: "/path/to/file.txt" -> "file.txt"
93+
if blob_name is None:
94+
blob_name = os.path.basename(local_file_path)
95+
96+
# Create a blob object representing the file in GCS
97+
# A blob is the basic storage unit in GCS (similar to a file)
98+
blob = bucket.blob(blob_name)
99+
100+
# Upload the file content to GCS
101+
# This method handles the actual file transfer
102+
blob.upload_from_filename(local_file_path)
103+
104+
# Log successful upload with full GCS path for reference
105+
self.log.info(
106+
f"File {local_file_path} uploaded to gs://{bucket_name}/{blob_name}"
107+
)

tests/test_gcp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88

99
from src.logger import Logger
10-
from src.gcp import GoogleStorageClient
10+
from src.bucket import GoogleStorageClient
1111
from google.cloud import storage
1212
from google.oauth2 import service_account
1313
from google.api_core.exceptions import NotFound

tests/test_llm_prompts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from main import ENVLoader, RunLLMPrompts
1010
from src.logger import Logger
11-
from src.gcp import GoogleStorageClient
11+
from src.bucket import GoogleStorageClient
1212
from google.cloud import storage
1313
from google.oauth2 import service_account
1414
from google.api_core.exceptions import NotFound

0 commit comments

Comments
 (0)