11"""Programmatically interact with a Google Cloud Storage bucket."""
2+
23from os import listdir
34from os .path import isfile , join
45from random import randint
5- from typing import List , Optional
6+ from typing import List , Optional , Tuple
67
78from google .cloud import storage
9+ from google .cloud .storage .blob import Blob
810
9- from config import bucket_dir , bucket_name , local_dir
11+ from config import BUCKET_DIR , BUCKET_NAME
1012
13+ # Initialize Google Cloud Storage client
1114storage_client = storage .Client ()
12- bucket = storage_client .get_bucket (bucket_name )
15+ bucket = storage_client .get_bucket (BUCKET_NAME )
1316
1417
15- def upload_files ( bucket_name : str , bucket_dir : str , local_dir : str ) -> str :
18+ def list_files ( ) -> List [ Optional [ str ]] :
1619 """
17- Upload files to GCP bucket.
20+ List all objects with file extension in a GCP bucket.
1821
1922 :param str bucket_name: Human-readable GCP bucket name.
2023 :param str bucket_dir: Bucket directory in which object exists.
21- :param str local_dir: Local file path to upload/download files.
2224
23- :returns: str
25+ :returns: List[Optional[ str]]
2426 """
25- files = [f for f in listdir (local_dir ) if isfile (join (local_dir , f ))]
26- for file in files :
27- local_file = local_dir + file
28- blob = bucket .blob (bucket_dir + file )
29- blob .upload_from_filename (local_file )
30- return f"Uploaded { files .join (', ' )} to '{ bucket_name } ' bucket."
27+ blobs = bucket .list_blobs (prefix = BUCKET_DIR )
28+ blob_file_list = [blob .name for blob in blobs if "." in blob .name ]
29+ return blob_file_list
3130
3231
33- def list_files ( bucket_dir : str ) -> List [ Optional [ str ]] :
32+ def pick_random_file ( ) -> str :
3433 """
35- List all objects with file extension in a GCP bucket.
34+ Pick a `random` file from GCP bucket.
3635
3736 :param str bucket_name: Human-readable GCP bucket name.
38- :param str bucket_dir: Bucket directory in which object exists.
3937
40- :returns: List[Optional[ str]]
38+ :returns: str
4139 """
42- files = bucket .list_blobs (prefix = bucket_dir )
43- file_list = [file .name for file in files if "." in file .name ]
44- return file_list
40+ blobs = list_files ()
41+ rand = randint (0 , len (blobs ) - 1 )
42+ blob = bucket .blob (blobs [rand ])
43+ return blob , blob .name
4544
4645
47- def download_random_file (bucket_name : str , local_dir : str ) -> str :
46+ def download_random_file (local_dir : str ) -> Tuple [ Blob , str ] :
4847 """
4948 Download random file from GCP bucket.
5049
@@ -54,15 +53,12 @@ def download_random_file(bucket_name: str, local_dir: str) -> str:
5453
5554 :returns: str
5655 """
57- fileList = list_files (bucket_name )
58- rand = randint (0 , len (fileList ) - 1 )
59- blob = bucket .blob (fileList [rand ])
60- file_name = blob .name .split ("/" )[- 1 ]
61- blob .download_to_filename (local_dir + file_name )
62- return f"{ file_name } downloaded from bucket."
56+ blob , blob_filename = pick_random_file ()
57+ blob .download_to_filename (f"{ local_dir } /{ blob .name .split ('/' )[- 1 ]} " )
58+ return blob , blob_filename
6359
6460
65- def delete_file (bucket_name : str , bucket_dir : str ) -> str :
61+ def delete_file (bucket_name : str ) -> str :
6662 """
6763 Delete file from GCP bucket.
6864
@@ -71,13 +67,12 @@ def delete_file(bucket_name: str, bucket_dir: str) -> str:
7167
7268 :returns: str
7369 """
74- blob = bucket .blob (bucket_dir + file_name )
75- file_name = blob .name .split ("/" )[- 1 ]
76- bucket .delete_blob (bucket_name + file_name )
77- return f"{ file_name } deleted from bucket: { bucket_name } ."
70+ blob , blob_filename = pick_random_file ()
71+ bucket .delete_blob (blob_filename )
72+ return f"{ blob_filename } deleted from bucket: { bucket_name } ."
7873
7974
80- def rename_file (bucket_dir : str , new_filename : str ) -> str :
75+ def rename_file (new_filename : str ) -> str :
8176 """
8277 Rename a file in GCP bucket.
8378
@@ -87,7 +82,25 @@ def rename_file(bucket_dir: str, new_filename: str) -> str:
8782
8883 :returns: str
8984 """
90- blob = bucket .blob (bucket_dir + file_name )
91- file_name = blob .name .split ("/" )[- 1 ]
85+ blob , blob_filename = pick_random_file ()
9286 bucket .rename_blob (blob , new_name = new_filename )
93- return f"{ file_name } renamed to { new_filename } ."
87+ return f"{ blob_filename } renamed to { new_filename } ."
88+
89+
90+ def upload_files (bucket_name : str , bucket_dir : str , local_dir : str ) -> str :
91+ """
92+ Upload files to GCP bucket.
93+
94+ :param str bucket_name: Human-readable GCP bucket name.
95+ :param str bucket_dir: Bucket directory in which object exists.
96+ :param str local_dir: Local file path to upload/download files.
97+
98+ :returns: str
99+ """
100+ files = [f for f in listdir (local_dir ) if isfile (join (local_dir , f ))]
101+ for file in files :
102+ local_file = f"{ local_dir } /{ file } "
103+ print (f"local file = { local_file } \n " )
104+ blob = bucket .blob (f"{ bucket_dir } /{ file } " )
105+ blob .upload_from_filename (local_file )
106+ return f"Uploaded { files } to '{ bucket_name } ' bucket."
0 commit comments