def write_to_gcs()

in genai-on-vertex-ai/gemini/evals_playbook/utils/evals_playbook.py [0:0]


def write_to_gcs(gcs_path, data):
    if not gcs_path.startswith("gs://"):
        raise Exception(f"Invalid Cloud Storage path {gcs_path}. Pass a valid path starting with gs://")

    # check if data is a file or a string
    UPLOAD_AS_FILE = False
    if os.path.exists(data):
        UPLOAD_AS_FILE = True

    bucket = gcs_path.split("/")[2]
    object = "/".join(gcs_path.split("/")[3:])
    
    # Initialize the Cloud Storage client
    storage_client = storage.Client()
    
    # Get the bucket object
    bucket = storage_client.bucket(bucket)
    blob = bucket.blob(object)
    if UPLOAD_AS_FILE:
        blob.upload_from_filename(data)
    else:
        blob.upload_from_string(data)
    return blob.self_link