-
Notifications
You must be signed in to change notification settings - Fork 0
/
qeep_api.py
55 lines (40 loc) · 1.11 KB
/
qeep_api.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
"""
api front-lambda interface
"""
import os
import io
import base64
from hashlib import md5
import json
import boto3
import filetype
INPUT_BUCKET = os.getenv("INPUT_BUCKET")
OUTPUT_BUCKET = os.getenv("OUTPUT_BUCKET")
def file_extension(file) -> str:
"""
Checks for the extension of passed file and
returns it as string
"""
kind = filetype.guess(file)
if kind is None:
return ""
return "." + kind.extension
def lambda_handler(event, _context):
"""
Lambda event handler
"""
s3_resource = boto3.resource("s3")
print(event)
buffer = base64.b64decode(event["body"])
io_buffer = io.BytesIO(buffer)
filename = md5(buffer).hexdigest() + file_extension(buffer)
location = boto3.client("s3").get_bucket_location(Bucket=INPUT_BUCKET)[
"LocationConstraint"
]
bucket = s3_resource.Object(INPUT_BUCKET, filename)
bucket.upload_fileobj(io_buffer)
outputlink = (
f"https://{OUTPUT_BUCKET}.s3-{location}.amazonaws.com/{filename}"
)
response = {"statusCode": 202, "outputLink": outputlink}
return json.dumps(response)