-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
41 lines (36 loc) · 1.05 KB
/
index.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
40
41
const aws = require("aws-sdk");
const fs = require("fs");
const path = require("path");
const spacesEndpoint = new aws.Endpoint(process.env.S3_ENDPOINT);
const s3 = new aws.S3({
endpoint: spacesEndpoint,
accessKeyId: process.env.S3_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
});
const uploadFile = (fileName) => {
if (fs.lstatSync(fileName).isDirectory()) {
fs.readdirSync(fileName).forEach((file) => {
uploadFile(`${fileName}/${file}`);
});
} else {
const fileContent = fs.readFileSync(fileName);
// Setting up S3 upload parameters
const params = {
Bucket: process.env.S3_BUCKET,
Key: `${process.env.S3_PREFIX || ""}/${path.normalize(fileName)}`,
Body: fileContent,
};
const acl = process.env.S3_ACL;
if (acl) {
params.ACL = acl;
}
// Uploading files to the bucket
s3.upload(params, function (err, data) {
if (err) {
throw err;
}
console.log(`File uploaded successful. ${data.Location}`);
});
}
};
uploadFile(process.env.FILE);