|
| 1 | +"""Programmatically interact with a Google Cloud Storage bucket.""" |
| 2 | +from os import listdir |
| 3 | +from os.path import isfile, join |
| 4 | +from random import randint |
| 5 | +from typing import List, Optional |
| 6 | + |
| 7 | +from google.cloud import storage |
| 8 | + |
| 9 | +from config import bucket_dir, bucket_name, local_dir |
| 10 | + |
| 11 | +storage_client = storage.Client() |
| 12 | +bucket = storage_client.get_bucket(bucket_name) |
| 13 | + |
| 14 | + |
| 15 | +def upload_files(bucket_name: str, bucket_dir: str, local_dir: str) -> str: |
| 16 | + """ |
| 17 | + Upload files to GCP bucket. |
| 18 | +
|
| 19 | + :param str bucket_name: Human-readable GCP bucket name. |
| 20 | + :param str bucket_dir: Bucket directory in which object exists. |
| 21 | + :param str local_dir: Local file path to upload/download files. |
| 22 | +
|
| 23 | + :returns: str |
| 24 | + """ |
| 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." |
| 31 | + |
| 32 | + |
| 33 | +def list_files(bucket_dir: str) -> List[Optional[str]]: |
| 34 | + """ |
| 35 | + List all objects with file extension in a GCP bucket. |
| 36 | +
|
| 37 | + :param str bucket_name: Human-readable GCP bucket name. |
| 38 | + :param str bucket_dir: Bucket directory in which object exists. |
| 39 | +
|
| 40 | + :returns: List[Optional[str]] |
| 41 | + """ |
| 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 |
| 45 | + |
| 46 | + |
| 47 | +def download_random_file(bucket_name: str, local_dir: str) -> str: |
| 48 | + """ |
| 49 | + Download random file from GCP bucket. |
| 50 | +
|
| 51 | + :param str bucket_name: Human-readable GCP bucket name. |
| 52 | + :param str bucket_dir: Bucket directory in which object exists. |
| 53 | + :param str local_dir: Local file path to upload/download files. |
| 54 | +
|
| 55 | + :returns: str |
| 56 | + """ |
| 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." |
| 63 | + |
| 64 | + |
| 65 | +def delete_file(bucket_name: str, bucket_dir: str) -> str: |
| 66 | + """ |
| 67 | + Delete file from GCP bucket. |
| 68 | +
|
| 69 | + :param str bucket_name: Human-readable GCP bucket name. |
| 70 | + :param str bucket_dir: Bucket directory in which object exists. |
| 71 | +
|
| 72 | + :returns: str |
| 73 | + """ |
| 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}." |
| 78 | + |
| 79 | + |
| 80 | +def rename_file(bucket_dir: str, new_filename: str) -> str: |
| 81 | + """ |
| 82 | + Rename a file in GCP bucket. |
| 83 | +
|
| 84 | + :param str bucket_name: Human-readable GCP bucket name. |
| 85 | + :param str bucket_dir: Bucket directory from which to extract object. |
| 86 | + :param str new_filename: New file name for Blob object. |
| 87 | +
|
| 88 | + :returns: str |
| 89 | + """ |
| 90 | + blob = bucket.blob(bucket_dir + file_name) |
| 91 | + file_name = blob.name.split("/")[-1] |
| 92 | + bucket.rename_blob(blob, new_name=new_filename) |
| 93 | + return f"{file_name} renamed to {new_filename}." |
0 commit comments