-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetReqLambda.js
39 lines (33 loc) · 1.09 KB
/
GetReqLambda.js
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
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const BUCKET_NAME = 'your-s3-bucket-name';
exports.handler = async (event) => {
try {
// List all JSON files in the S3 bucket
const listParams = {
Bucket: BUCKET_NAME
};
const listedObjects = await s3.listObjectsV2(listParams).promise();
const allData = [];
// Retrieve each JSON file and append its contents to allData
for (const item of listedObjects.Contents) {
const getObjectParams = {
Bucket: BUCKET_NAME,
Key: item.Key
};
const fileObject = await s3.getObject(getObjectParams).promise();
const fileContent = JSON.parse(fileObject.Body.toString('utf-8'));
allData.push(fileContent);
}
return {
statusCode: 200,
body: JSON.stringify(allData)
};
} catch (error) {
console.error(error);
return {
statusCode: 500,
body: JSON.stringify({ error: 'Failed to retrieve JSON data' })
};
}
};