Untitled

 avatar
unknown
plain_text
7 days ago
1.4 kB
1
Indexable
from google.cloud import storage
import os

def download_gcs_directory(bucket_name, gcs_path, local_path, credentials_path):
    """
    Downloads all files and folders from a GCS path to a local directory using custom credentials.
    
    :param bucket_name: Name of the GCS bucket
    :param gcs_path: Path within the GCS bucket (prefix)
    :param local_path: Local directory to save the files
    :param credentials_path: Path to the Google Cloud credentials JSON file
    """
    client = storage.Client.from_service_account_json(credentials_path)
    bucket = client.bucket(bucket_name)
    blobs = bucket.list_blobs(prefix=gcs_path)
    
    for blob in blobs:
        if blob.name.split(".")[-1] == 'pkl':  # Fix `blob.path` to `blob.name`
            local_file_path = os.path.join(local_path, blob.name.split("/")[-1])
            os.makedirs(os.path.dirname(local_file_path), exist_ok=True)
            blob.download_to_filename(local_file_path)
            print(f"Downloaded {blob.name} to {local_file_path}")

if __name__ == "__main__":
    bucket_name = "quant-research-data"
    gcs_path = "MAIN/NIFTY50/OPTIONS/"  # Prefix for the files to download
    local_path = "./data"  # Local destination
    credentials_path = "/Users/sachinmathew/marketfeed/credentials/marketfeed-stage.json"  # Path to service account key

    download_gcs_directory(bucket_name, gcs_path, local_path, credentials_path)
Leave a Comment