-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathlambda_api_pre_signed_url.py
More file actions
44 lines (36 loc) · 1.38 KB
/
lambda_api_pre_signed_url.py
File metadata and controls
44 lines (36 loc) · 1.38 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
# -*- coding: utf-8 -*-
__author__ = "Chirag Rathod (Srce Cde)"
__license__ = "MIT"
__email__ = "chiragr83@gmail.com"
__maintainer__ = "Chirag Rathod (Srce Cde)"
import json
import base64
import boto3
def lambda_handler(event, context):
s3 = boto3.client("s3")
# For fetching bucket_name & file_name using proxy integration method from API Gateway
bucket_name = event["pathParameters"]["bucket"]
file_name = event["queryStringParameters"]["file"]
# For fetching bucket_name & file_name using legacy method from API Gateway
bucket_name = event["params"]["path"]["bucket"]
file_name = event["params"]["querystring"]["file"]
"""
1. Generate pre-signed URL for downloading file
2. Replace get_object with put_object for generating pre-signed URL to upload file
3. Use PUT method while uploading file using Pre-Signed URL
"""
URL = s3.generate_presigned_url(
"get_object", Params={"Bucket": bucket_name, "Key": file_name}, ExpiresIn=3600
)
"""
1. Generate pre-signed URL for downloading file
2. Use POST method while uploading file using Pre-Signed URL
"""
URL = s3.generate_presigned_post(
Bucket=bucket_name, Key=file_name, Fields=None, Conditions=None, ExpiresIn=3600
)
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps({"URL": URL}),
}