/
shipment-logo.py
73 lines (59 loc) · 2.36 KB
/
shipment-logo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import boto3
import mysql.connector
import os
client = boto3.client('s3')
# MySQL credentials
cnx = mysql.connector.connect(
host=os.environ['MSQL_HOST'],
user=os.environ['MSQL_USER'],
password=os.environ['MSQL_PASS'],
database=os.environ['MYSQL_DB']
)
# MySQL query to grab the logos for the shipment orders
cursor = cnx.cursor()
cursor.execute("SELECT concat(Vendor_ID, '_logo.jpg') AS vendor_id_logo, concat(upper(Company_Code), '/logo.jpg') "
"As Company_ID_logo, is_factory, vendor_internal_id, concat(upper(Company_Code), '/logo.png') "
"AS Company_ID_logo_2, concat(upper(Company_Code), '/logo.JPG') as Company_ID_logo_3 "
"FROM ss2_migration_latest_0603.SS1_company_code_linkages2;")
linkage = cursor.fetchall()
cnx.close()
# Use AWS API to list objects in buckets and store results into a list
list_of_objects = client.list_objects_v2(
Bucket='company-logo-live'
)
keys = []
for logos in list_of_objects['Contents']:
keys.append(logos['Key'])
# Condition to determine the seller type(FD / Non-FD), then find a match and copy to new bucket
for key in keys:
for link in linkage:
if key in link:
if link[2] == 1 and link[3] is not None:
response = client.copy_object(
Bucket='ss2-us-public-live',
CopySource=f'company-logo-live/{key}',
Key=f'factory-logo/{link[0]}',
)
response2 = client.copy_object(
Bucket='ss2-us-public-live',
CopySource=f'company-logo-live/{key}',
Key=f'seller-logo/{link[0]}',
)
print(response, response2)
continue
elif link[2] == 1:
response = client.copy_object(
Bucket='ss2-us-public-live',
CopySource=f'company-logo-live/{key}',
Key=f'factory-logo/{link[0]}',
)
print(response)
continue
elif link[2] == 0:
response = client.copy_object(
Bucket='ss2-us-public-live', # Destination bucket
CopySource=f'company-logo-live/{key}',
Key=f'seller-logo/{link[0]}',
)
print(response)
continue