LogoGeneration / app /utils /s3_utils.py
utkarsh134's picture
Upload 3 files
1235a65 verified
Raw History Blame Contribute Delete
2.71 kB
# made for adding s3 storage, no longer in need due to reach storage
# import uuid
# from io import BytesIO
# import boto3
# from botocore.exceptions import ClientError
# from app.config.settings import settings
# def get_s3_client():
# return boto3.client(
# "s3",
# aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
# aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
# region_name=settings.AWS_REGION,
# )
# def upload_image_to_s3(image_data, brand_name, image_format, index):
# try:
# s3_client = get_s3_client()
# unique_id = str(uuid.uuid4())[:8]
# filename = f"{brand_name.replace(' ', '_')}_{index}_{unique_id}.{image_format}"
# s3_client.upload_fileobj(
# BytesIO(image_data),
# settings.S3_BUCKET_NAME,
# filename,
# ExtraArgs={"ContentType": f"image/{image_format}"},
# )
# url = f"https://{settings.S3_BUCKET_NAME}.s3.{settings.AWS_REGION}.amazonaws.com/{filename}"
# return {
# "id": f"logo_{index}",
# "url": url,
# "format": image_format,
# "name": filename,
# }
# except ClientError as e:
# raise Exception(f"S3 upload error: {str(e)}")
# def upload_logo_to_s3(image_data: bytes, brand_name: str, image_format: str, index: int):
# try:
# s3_client = get_s3_client()
# unique_id = str(uuid.uuid4())[:8]
# filename = f"{brand_name.replace(' ', '')}{index}_{unique_id}.{image_format.lower()}"
# if image_format.lower() == "svg":
# extra_args = {
# "ContentType": "image/svg+xml",
# "ContentDisposition": "inline",
# }
# elif image_format.lower() == "ico":
# extra_args = {
# "ContentType": "image/x-icon",
# "ContentDisposition": "inline",
# }
# else:
# extra_args = {
# "ContentType": f"image/{image_format.lower()}",
# }
# s3_client.upload_fileobj(
# BytesIO(image_data),
# settings.S3_BUCKET_NAME,
# filename,
# ExtraArgs=extra_args,
# )
# url = f"https://{settings.S3_BUCKET_NAME}.s3.{settings.AWS_REGION}.amazonaws.com/{filename}"
# return {
# "id": f"logo_{index}",
# "url": url,
# "format": image_format,
# "name": filename,
# }
# except ClientError as e:
# raise Exception(f"S3 upload error: {str(e)}")