-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathlambda_api_download_presigned.py
More file actions
48 lines (41 loc) · 1.23 KB
/
lambda_api_download_presigned.py
File metadata and controls
48 lines (41 loc) · 1.23 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
48
"""
-*- coding: utf-8 -*-
========================
AWS Lambda
========================
Contributor: Chirag Rathod (Srce Cde)
========================
"""
import os
import logging
import boto3
from botocore.exceptions import ClientError
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
s3_client = boto3.client("s3")
try:
BUCKET_NAME = os.environ["BUCKET_NAME"]
except Exception as e:
logger.error("ENV variable is not defined")
if event and BUCKET_NAME:
logger.info(f"Event: {event}")
object_name = event.get("queryStringParameters").get("guide")
try:
response = s3_client.generate_presigned_url(
"get_object",
Params={"Bucket": BUCKET_NAME, "Key": f"guide/{object_name}"},
ExpiresIn=3600,
)
except ClientError as e:
logging.error(e)
return {
"statusCode": 500,
"headers": {"Content-Type": "application/json"},
"body": "Error processing the request",
}
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": response,
}