utkarsh134 commited on
Commit
1235a65
·
verified ·
1 Parent(s): 556c16f

Upload 3 files

Browse files
Files changed (2) hide show
  1. app/utils/reach_upload.py +22 -11
  2. app/utils/s3_utils.py +75 -72
app/utils/reach_upload.py CHANGED
@@ -6,12 +6,22 @@ from io import BytesIO
6
  import logging
7
 
8
  logger = logging.getLogger(__name__)
 
 
 
 
 
 
 
 
 
9
 
10
  def upload_to_reach(
11
  image_data: bytes,
12
  brand_name: str,
13
  image_format: str,
14
  index: int,
 
15
  max_retries: int = 3,
16
  retry_delay: float = 2.0
17
  ) -> Dict[str, str]:
@@ -23,6 +33,7 @@ def upload_to_reach(
23
  brand_name (str): Brand name for reference
24
  image_format (str): Image format/extension
25
  index (int): Index to differentiate multiple uploads
 
26
  max_retries (int): Maximum number of retry attempts
27
  retry_delay (float): Delay between retries in seconds
28
 
@@ -45,13 +56,9 @@ def upload_to_reach(
45
 
46
  for attempt in range(max_retries):
47
  try:
48
- # Create file-like object from bytes
49
  file_obj = BytesIO(image_data)
50
-
51
- # Create filename
52
  filename = f"{brand_name.replace(' ', '')}_{index}.{image_format.lower()}"
53
-
54
- # Prepare the file for upload
55
  files = {
56
  'file': (filename, file_obj, f'image/{image_format.lower()}')
57
  }
@@ -64,20 +71,24 @@ def upload_to_reach(
64
  'x-partner-tenant-id': 'REACH@skyhawk'
65
  }
66
 
67
- logger.info(f"Attempting upload of {filename} (attempt {attempt + 1}/{max_retries})")
 
 
 
 
 
68
 
69
  response = requests.post(
70
- f"{url}?fileType=LOGO",
71
- files=files,
72
  headers=headers,
73
- timeout=30 # Add timeout parameter
74
  )
75
 
76
  if response.status_code != 200:
77
  raise requests.HTTPError(f"Upload failed: {response.status_code} {response.text}")
78
 
79
  response_data = response.json()
80
-
81
  if response_data.get('status') != 'SUCCESS':
82
  raise requests.HTTPError(f"Upload failed: {response_data}")
83
 
@@ -96,4 +107,4 @@ def upload_to_reach(
96
 
97
  error_msg = f"❌ Error uploading to Reach platform after {max_retries} attempts: {str(last_exception)}"
98
  logger.error(error_msg)
99
- raise requests.RequestException(error_msg)
 
6
  import logging
7
 
8
  logger = logging.getLogger(__name__)
9
+ logger.setLevel(logging.DEBUG)
10
+
11
+ # Add a stream handler if not already added
12
+ if not logger.handlers:
13
+ handler = logging.StreamHandler()
14
+ handler.setLevel(logging.DEBUG)
15
+ formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
16
+ handler.setFormatter(formatter)
17
+ logger.addHandler(handler)
18
 
19
  def upload_to_reach(
20
  image_data: bytes,
21
  brand_name: str,
22
  image_format: str,
23
  index: int,
24
+ file_type: str, # Required parameter
25
  max_retries: int = 3,
26
  retry_delay: float = 2.0
27
  ) -> Dict[str, str]:
 
33
  brand_name (str): Brand name for reference
34
  image_format (str): Image format/extension
35
  index (int): Index to differentiate multiple uploads
36
+ file_type (str): Type of file to upload (e.g., LOGO, FAV_ICON, etc.)
37
  max_retries (int): Maximum number of retry attempts
38
  retry_delay (float): Delay between retries in seconds
39
 
 
56
 
57
  for attempt in range(max_retries):
58
  try:
 
59
  file_obj = BytesIO(image_data)
 
 
60
  filename = f"{brand_name.replace(' ', '')}_{index}.{image_format.lower()}"
61
+
 
62
  files = {
63
  'file': (filename, file_obj, f'image/{image_format.lower()}')
64
  }
 
71
  'x-partner-tenant-id': 'REACH@skyhawk'
72
  }
73
 
74
+ logger.info(f"Attempting upload of {filename} as {file_type} (attempt {attempt + 1}/{max_retries})")
75
+
76
+ # Ensure file_type is present in the URL
77
+ upload_url = f"{url}?fileType={file_type}"
78
+
79
+ logger.debug(f"Uploading to endpoint: {upload_url}")
80
 
81
  response = requests.post(
82
+ upload_url,
83
+ files=files,
84
  headers=headers,
85
+ timeout=30
86
  )
87
 
88
  if response.status_code != 200:
89
  raise requests.HTTPError(f"Upload failed: {response.status_code} {response.text}")
90
 
91
  response_data = response.json()
 
92
  if response_data.get('status') != 'SUCCESS':
93
  raise requests.HTTPError(f"Upload failed: {response_data}")
94
 
 
107
 
108
  error_msg = f"❌ Error uploading to Reach platform after {max_retries} attempts: {str(last_exception)}"
109
  logger.error(error_msg)
110
+ raise requests.RequestException(error_msg)
app/utils/s3_utils.py CHANGED
@@ -1,86 +1,89 @@
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)}")
45
 
46
- def upload_logo_to_s3(image_data: bytes, brand_name: str, image_format: str, index: int):
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- try:
49
- s3_client = get_s3_client()
50
-
51
- unique_id = str(uuid.uuid4())[:8]
52
- filename = f"{brand_name.replace(' ', '')}{index}_{unique_id}.{image_format.lower()}"
53
-
54
- if image_format.lower() == "svg":
55
- extra_args = {
56
- "ContentType": "image/svg+xml",
57
- "ContentDisposition": "inline",
58
- }
59
- elif image_format.lower() == "ico":
60
- extra_args = {
61
- "ContentType": "image/x-icon",
62
- "ContentDisposition": "inline",
63
- }
64
- else:
65
- extra_args = {
66
- "ContentType": f"image/{image_format.lower()}",
67
- }
68
-
69
- s3_client.upload_fileobj(
70
- BytesIO(image_data),
71
- settings.S3_BUCKET_NAME,
72
- filename,
73
- ExtraArgs=extra_args,
74
- )
75
-
76
- url = f"https://{settings.S3_BUCKET_NAME}.s3.{settings.AWS_REGION}.amazonaws.com/{filename}"
77
-
78
- return {
79
- "id": f"logo_{index}",
80
- "url": url,
81
- "format": image_format,
82
- "name": filename,
83
- }
84
-
85
- except ClientError as e:
86
- raise Exception(f"S3 upload error: {str(e)}")
 
1
+ # made for adding s3 storage, no longer in need due to reach storage
 
2
 
 
 
3
 
4
+ # import uuid
5
+ # from io import BytesIO
6
 
7
+ # import boto3
8
+ # from botocore.exceptions import ClientError
9
 
10
+ # from app.config.settings import settings
11
 
 
 
 
 
 
 
12
 
13
+ # def get_s3_client():
14
 
15
+ # return boto3.client(
16
+ # "s3",
17
+ # aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
18
+ # aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
19
+ # region_name=settings.AWS_REGION,
20
+ # )
21
 
 
 
22
 
23
+ # def upload_image_to_s3(image_data, brand_name, image_format, index):
 
24
 
25
+ # try:
26
+ # s3_client = get_s3_client()
 
 
 
 
27
 
28
+ # unique_id = str(uuid.uuid4())[:8]
29
+ # filename = f"{brand_name.replace(' ', '_')}_{index}_{unique_id}.{image_format}"
30
 
31
+ # s3_client.upload_fileobj(
32
+ # BytesIO(image_data),
33
+ # settings.S3_BUCKET_NAME,
34
+ # filename,
35
+ # ExtraArgs={"ContentType": f"image/{image_format}"},
36
+ # )
 
 
37
 
38
+ # url = f"https://{settings.S3_BUCKET_NAME}.s3.{settings.AWS_REGION}.amazonaws.com/{filename}"
39
+
40
+ # return {
41
+ # "id": f"logo_{index}",
42
+ # "url": url,
43
+ # "format": image_format,
44
+ # "name": filename,
45
+ # }
46
+ # except ClientError as e:
47
+ # raise Exception(f"S3 upload error: {str(e)}")
48
+
49
+ # def upload_logo_to_s3(image_data: bytes, brand_name: str, image_format: str, index: int):
50
 
51
+ # try:
52
+ # s3_client = get_s3_client()
53
+
54
+ # unique_id = str(uuid.uuid4())[:8]
55
+ # filename = f"{brand_name.replace(' ', '')}{index}_{unique_id}.{image_format.lower()}"
56
+
57
+ # if image_format.lower() == "svg":
58
+ # extra_args = {
59
+ # "ContentType": "image/svg+xml",
60
+ # "ContentDisposition": "inline",
61
+ # }
62
+ # elif image_format.lower() == "ico":
63
+ # extra_args = {
64
+ # "ContentType": "image/x-icon",
65
+ # "ContentDisposition": "inline",
66
+ # }
67
+ # else:
68
+ # extra_args = {
69
+ # "ContentType": f"image/{image_format.lower()}",
70
+ # }
71
+
72
+ # s3_client.upload_fileobj(
73
+ # BytesIO(image_data),
74
+ # settings.S3_BUCKET_NAME,
75
+ # filename,
76
+ # ExtraArgs=extra_args,
77
+ # )
78
+
79
+ # url = f"https://{settings.S3_BUCKET_NAME}.s3.{settings.AWS_REGION}.amazonaws.com/{filename}"
80
+
81
+ # return {
82
+ # "id": f"logo_{index}",
83
+ # "url": url,
84
+ # "format": image_format,
85
+ # "name": filename,
86
+ # }
87
+
88
+ # except ClientError as e:
89
+ # raise Exception(f"S3 upload error: {str(e)}")