-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
100 lines (82 loc) · 2.59 KB
/
lambda_function.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import base64
import boto3
import os
import uuid
from concurrent.futures import ThreadPoolExecutor
def handle_error(e):
print(e)
return {
"result": "FAIL",
"data": None,
"message": "사진이 업로드 되지 않았습니다.",
"errorCode": "INTERNAL_SERVER_ERROR"
}
def upload_image(image, resources):
try:
AWS_BUCKET_NAME = resources['AWS_BUCKET_NAME']
bucket = resources['bucket']
bucket_location = resources['bucket_location']
location = 'images/'
if image["location"] is not None:
location = image["location"] + '/'
path = location + str(uuid.uuid4()) + '.jpg'
print("path set end")
imageData = base64.b64decode(image["imageData"])
print("imageData: ", imageData)
bucket.put_object(
ACL='public-read',
Key=path,
Body=imageData,
)
imageUrl = "https://s3-{0}.amazonaws.com/{1}/{2}".format(
bucket_location['LocationConstraint'],
AWS_BUCKET_NAME,
path)
return {
"result": "SUCCESS",
"data": {
"imageUrl": imageUrl,
"s3_path": path
},
"message": "이미지 업로드에 성공했습니다",
"errorCode": None
}
except Exception as e:
return handle_error(e)
def lambda_handler(event, context):
try:
print(event)
images = event.get("images", [])
if len(images) == 0:
raise Exception("No images to upload")
if len(images) > 10:
raise Exception("Too many images")
except Exception as e:
return handle_error(e)
AWS_BUCKET_NAME = os.environ.get('AWS_BUCKET_NAME')
s3 = boto3.resource('s3')
bucket = s3.Bucket(AWS_BUCKET_NAME)
bucket_location = boto3.client('s3').get_bucket_location(Bucket=AWS_BUCKET_NAME)
resources = {
'AWS_BUCKET_NAME': AWS_BUCKET_NAME,
'bucket': bucket,
'bucket_location': bucket_location
}
with ThreadPoolExecutor(max_workers=50) as executor:
results = list(executor.map(lambda image: upload_image(image, resources), images))
if any(result["result"] == "FAIL" for result in results):
successful_uploads = [result["data"]["s3_path"] for result in results if result["result"] == "SUCCESS"]
if successful_uploads:
print(f"deleting : {successful_uploads}")
print(bucket.delete_objects(
Delete={
'Objects': [{'Key': path} for path in successful_uploads]
}
))
return handle_error(Exception("One or more images failed to upload"))
return {
"result": "SUCCESS",
"data": [{"imageUrl": result["data"]["imageUrl"]} for result in results],
"message": "이미지 업로드에 성공했습니다",
"errorCode": None
}