-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathS3.js
More file actions
40 lines (31 loc) · 930 Bytes
/
S3.js
File metadata and controls
40 lines (31 loc) · 930 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
37
38
39
40
const AWS = require('aws-sdk');
const s3Client = new AWS.S3();
const S3 = {
async get(fileName, bucket) {
const params = {
Bucket: bucket,
Key: fileName,
};
let data = await s3Client.getObject(params).promise();
if (!data) {
throw Error(`Failed to get file ${fileName}, from ${bucket}`);
}
if (fileName.slice(fileName.length - 4, fileName.length) == 'json') {
data = data.Body.toString();
}
return data;
},
async write(data, fileName, bucket) {
const params = {
Bucket: bucket,
Body: JSON.stringify(data),
Key: fileName,
};
const newData = await s3Client.putObject(params).promise();
if (!newData) {
throw Error('there was an error writing the file');
}
return newData;
},
};
module.exports = S3;