|
| 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 | + ) |
0 commit comments