-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathlambda_api_download_binary_data.py
More file actions
50 lines (44 loc) · 1.42 KB
/
lambda_api_download_binary_data.py
File metadata and controls
50 lines (44 loc) · 1.42 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
49
50
"""
-*- coding: utf-8 -*-
========================
AWS Lambda
========================
Contributor: Chirag Rathod (Srce Cde)
========================
"""
import os
import logging
import json
import base64
import boto3
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
logger.info(event)
try:
bucket_name = os.environ.get("BUCKET_NAME", None)
if event and bucket_name:
s3 = boto3.client("s3")
folder_name = event.get("pathParameters").get("folder")
file_name = event.get("queryStringParameters").get("file")
fileObj = s3.get_object(
Bucket=bucket_name, Key=f"{folder_name}/{file_name}"
)
file_content = fileObj["Body"].read()
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/pdf",
"Content-Disposition": "attachment; filename={}".format(file_name),
},
"body": base64.b64encode(file_content),
"isBase64Encoded": True,
}
else:
return {
"statusCode": 500,
"body": json.dumps("Invalid invocation or Bucket name is not defined!"),
}
except Exception as e:
logger.error(e)
return {"statusCode": 500, "body": json.dumps("Error processing the request!")}