-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-url.py
53 lines (45 loc) · 2.24 KB
/
upload-url.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
#
# This software is Copyright ©️ 2020 The University of Southern California. All Rights Reserved.
# Permission to use, copy, modify, and distribute this software and its documentation for educational, research and non-profit purposes, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and subject to the full license file found in the root of this software deliverable. Permission to make commercial use of this software may be obtained by contacting: USC Stevens Center for Innovation University of Southern California 1150 S. Olive Street, Suite 2300, Los Angeles, CA 90115, USA Email: [email protected]
#
# The full terms of this copyright and license should always be found in the root directory of this software deliverable as "license.txt" and if these terms are not found with this software, please contact the USC Stevens Center for the full license.
#
import boto3
import json
import uuid
from module.logger import get_logger
from module.utils import create_json_response, load_sentry, require_env
load_sentry()
log = get_logger("upload-url")
upload_bucket = require_env("SIGNED_UPLOAD_BUCKET")
aws_region = require_env("REGION")
s3_client = boto3.client("s3", region_name=aws_region)
def handler(event, context):
log.info("creating signed url")
token = json.loads(event["requestContext"]["authorizer"]["token"])
id = uuid.uuid4()
object_name = f"{token['id']}/{id}"
# https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-HTTPPOSTConstructPolicy.html
signedUrl = s3_client.generate_presigned_post(
upload_bucket,
object_name,
Fields={"key": object_name},
Conditions=[
["content-length-range", 0, 104_857_600], # 100MB
# ["starts-with", "$Content-Type", "video/"],
],
ExpiresIn=3600,
)
data = {"url": signedUrl["url"], "fields": signedUrl["fields"]}
return create_json_response(200, data, event)
# # for local debugging:
# if __name__ == "__main__":
# event = {
# "requestContext": {
# "authorizer": {
# "token": "{\"id\": \"6196af5e068d43dc686194ed\"}"
# }
# }
# }
# response = handler(event, {})
# print(json.dumps(response))