-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathlambda_api_post_presigned.py
More file actions
47 lines (40 loc) · 1.3 KB
/
lambda_api_post_presigned.py
File metadata and controls
47 lines (40 loc) · 1.3 KB
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
"""
-*- coding: utf-8 -*-
========================
AWS Lambda
========================
Contributor: Chirag Rathod (Srce Cde)
========================
"""
import os
import logging
import json
import boto3
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
logger.info(event)
bucket_name = os.environ.get("BUCKET_NAME", None)
try:
if event and bucket_name:
s3 = boto3.client("s3")
user_name = event.get("pathParameters").get("username")
file_name = event.get("queryStringParameters").get("filename")
response = s3.generate_presigned_post(
Bucket=bucket_name,
Key=f"{user_name}/{file_name}",
Fields={"Content-Type": "image/jpg"},
Conditions=[
["starts-with", "$Content-Type", "image/"],
["content-length-range", 0, 10485760],
],
ExpiresIn=3600,
)
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps(response),
}
except Exception as e:
logger.error(e)
return {"statusCode": 500, "body": json.dumps("Error processing the request!")}