forked from WaveAqualei/TestingGrounds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
36 lines (34 loc) · 993 Bytes
/
storage.js
File metadata and controls
36 lines (34 loc) · 993 Bytes
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
const { S3Client, PutObjectCommand, GetObjectCommand, ListObjectsV2Command } = require("@aws-sdk/client-s3");
const s3Client = new S3Client({ region: 'us-east-2' });
function streamToString(stream) {
return new Promise((resolve, reject) => {
const chunks = [];
stream.on("data", (chunk) => chunks.push(chunk));
stream.on("error", reject);
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
});
}
module.exports = {
list: function() {
return s3Client.send(new ListObjectsV2Command({
Bucket: process.env.S3_BUCKET_NAME,
})).then(function(data) {
return data.Contents;
});
},
load: function(filename) {
return s3Client.send(new GetObjectCommand({
Bucket: process.env.S3_BUCKET_NAME,
Key: filename,
})).then(function(data) {
return streamToString(data.Body);
});
},
store: function(filename, data) {
return s3Client.send(new PutObjectCommand({
Bucket: process.env.S3_BUCKET_NAME,
Key: filename,
Body: data,
}));
},
};