utils.py 471 B

1234567891011121314151617
  1. import os
  2. from google.cloud import storage
  3. from werkzeug.utils import secure_filename
  4. import uuid
  5. def upload_file_to_gcs(file, bucket_name):
  6. """Uploads a file to the bucket."""
  7. storage_client = storage.Client()
  8. bucket = storage_client.bucket(bucket_name)
  9. filename = secure_filename(file.filename)
  10. unique_filename = f"{uuid.uuid4()}_{filename}"
  11. blob = bucket.blob(unique_filename)
  12. blob.upload_from_file(file)
  13. return blob.public_url