utkarsh134 commited on
Commit
bb2bf7e
·
verified ·
1 Parent(s): 15a3b8d

Upload 3 files

Browse files
app/__init__.py ADDED
File without changes
app/utils/__init__.py ADDED
File without changes
app/utils/s3_utils.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import uuid
2
+ from io import BytesIO
3
+
4
+ import boto3
5
+ from botocore.exceptions import ClientError
6
+
7
+ from app.config.settings import settings
8
+
9
+
10
+ def get_s3_client():
11
+
12
+ return boto3.client(
13
+ "s3",
14
+ aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
15
+ aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
16
+ region_name=settings.AWS_REGION,
17
+ )
18
+
19
+
20
+ def upload_image_to_s3(image_data, brand_name, image_format, index):
21
+
22
+ try:
23
+ s3_client = get_s3_client()
24
+
25
+ unique_id = str(uuid.uuid4())[:8]
26
+ filename = f"{brand_name.replace(' ', '_')}_{index}_{unique_id}.{image_format}"
27
+
28
+ s3_client.upload_fileobj(
29
+ BytesIO(image_data),
30
+ settings.S3_BUCKET_NAME,
31
+ filename,
32
+ ExtraArgs={"ContentType": f"image/{image_format}"},
33
+ )
34
+
35
+ url = f"https://{settings.S3_BUCKET_NAME}.s3.{settings.AWS_REGION}.amazonaws.com/{filename}"
36
+
37
+ return {
38
+ "id": f"logo_{index}",
39
+ "url": url,
40
+ "format": image_format,
41
+ "name": filename,
42
+ }
43
+ except ClientError as e:
44
+ raise Exception(f"S3 upload error: {str(e)}")