-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcs_utils.py
More file actions
26 lines (20 loc) · 959 Bytes
/
gcs_utils.py
File metadata and controls
26 lines (20 loc) · 959 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from google.cloud import storage
import os
from google.oauth2 import service_account
import io
import config
# --- Configure Google Cloud Storage ---
PROJECT_ID = config.PROJECT_ID
BUCKET_NAME = config.BUCKET_NAME
CREDENTIALS_PATH = <YOUR-CREDENTIALS-JSON>
def upload_file_to_gcs(file_bytes, filename): # More general function name
"""Uploads a file to Google Cloud Storage."""
try:
storage_client = storage.Client(project=PROJECT_ID) # Let Google Cloud find credentials
bucket = storage_client.bucket(BUCKET_NAME)
blob = bucket.blob(filename)
# Upload from bytes, automatically detecting content type if possible
blob.upload_from_string(file_bytes) # No need to specify content_type manually
return f"File '{filename}' uploaded successfully to GCS.", f"gs://{BUCKET_NAME}/{filename}"
except Exception as e:
return f"Error uploading file to GCS: {type(e).__name__} - {str(e)}", None