-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3test.js
71 lines (61 loc) · 1.89 KB
/
s3test.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// function uploadToS3() {
// let ssPath = "./Screenshots/new/cnn_page.png"
// fs.readFile(ssPath, function (err,data) {
// if (err) {
// return console.log(err);
// }
// let imageStream = fs.createReadStream(ssPath)
// let myBucket = 'kalefive.unique.bucket.name';
// let myKey = 'qa/' + 'cnnImage.png';
// let params = {
// Bucket: myBucket,
// Key: myKey,
// Body: imageStream,
// ACL: 'public-read',
// };
// s3.putObject(params)
// });
// };
// uploadToS3()
// Bulk upload whole directory to S3
// https://gist.github.com/jlouros/9abc14239b0d9d8947a3345b99c4ebcb
const path = require("path"); // from node.js
// configuration
const config = {
s3BucketName: "kalefive.unique.bucket.name",
folderPath: "./screenshots/new" // path relative script's location
};
// resolve full folder path
const distFolderPath = path.join(__dirname, config.folderPath);
// get of list of files from 'dist' directory
fs.readdir(distFolderPath, (err, files) => {
if(!files || files.length === 0) {
console.log(`provided folder '${distFolderPath}' is empty or does not exist.`);
console.log('Make sure your project was compiled!');
return;
}
// for each file in the directory
for (const fileName of files) {
// get the full path of the file
const filePath = path.join(distFolderPath, fileName);
// ignore if directory
if (fs.lstatSync(filePath).isDirectory()) {
continue;
}
// read file contents
fs.readFile(filePath, (error, fileContent) => {
// if unable to read file contents, throw exception
if (error) { throw error; }
// upload file to S3
s3.putObject({
Bucket: config.s3BucketName,
Key: "new/" + fileName,
Body: fileContent,
ACL: "public-read"
}, (res) => {
console.log(`Successfully uploaded new/'${fileName}'!`);
});
});
}
});