kopyl

Untitled

May 28th, 2023 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. import os
  2. import io
  3.  
  4.  
  5. AWS_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID")
  6. AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY")
  7. S3_BUCKET_NAME = "S3_BUCKET_NAME"
  8.  
  9. s3 = boto3.client("s3")
  10. bucket_name = S3_BUCKET_NAME
  11. s3 = boto3.client(
  12.     "s3",
  13.     aws_access_key_id=AWS_ACCESS_KEY_ID,
  14.     aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
  15. )
  16.  
  17. def upload_base64_image_list_to_s3(base64_image_list):
  18.     urls_list = []
  19.     for i, base64_image in enumerate(base64_image_list):
  20.         image_bytes = base64.b64decode(base64_image)
  21.         image = Image.open(io.BytesIO(image_bytes))
  22.         file_name = f"{uuid.uuid4()}.png"
  23.         image.save(file_name)
  24.         s3.upload_file(file_name, bucket_name, file_name)
  25.         os.remove(file_name)
  26.         file_url = f"https://{bucket_name}.s3.amazonaws.com/{file_name}"
  27.         urls_list.append(file_url)
  28.     return urls_list
Add Comment
Please, Sign In to add comment