-
Notifications
You must be signed in to change notification settings - Fork 336
/
Copy pathlambda_api_multipart.py
56 lines (47 loc) · 1.78 KB
/
lambda_api_multipart.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
# -*- coding: utf-8 -*-
__author__ = "Chirag Rathod (Srce Cde)"
__license__ = "MIT"
__email__ = "[email protected]"
__maintainer__ = "Chirag Rathod (Srce Cde)"
import json
import base64
import boto3
import email
def lambda_handler(event, context):
s3 = boto3.client("s3")
# decoding form-data into bytes
post_data = base64.b64decode(event["body"])
# fetching content-type
try:
content_type = event["headers"]["Content-Type"]
except:
content_type = event["headers"]["content-type"]
# concate Content-Type: with content_type from event
ct = "Content-Type: " + content_type + "\n"
# parsing message from bytes
msg = email.message_from_bytes(ct.encode() + post_data)
# checking if the message is multipart
print("Multipart check : ", msg.is_multipart())
# if message is multipart
if msg.is_multipart():
multipart_content = {}
# retrieving form-data
for part in msg.get_payload():
# checking if filename exist as a part of content-disposition header
if part.get_filename():
# fetching the filename
file_name = part.get_filename()
multipart_content[
part.get_param("name", header="content-disposition")
] = part.get_payload(decode=True)
# filename from form-data
file_name = json.loads(multipart_content["Metadata"])["filename"]
# u uploading file to S3
s3_upload = s3.put_object(
Bucket="bucket-name", Key=file_name, Body=multipart_content["file"]
)
# on upload success
return {"statusCode": 200, "body": json.dumps("File uploaded successfully!")}
else:
# on upload failure
return {"statusCode": 500, "body": json.dumps("Upload failed!")}