-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.js
55 lines (49 loc) · 1.42 KB
/
aws.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
var fs = require('fs');
var aws = require('aws-sdk');
require('dotenv').load();
var params = {accessKeyId : process.env.S3_ACCESS_KEY_ID, secretAccessKey: process.env.S3_SECRET_ACCESS_KEY};
var s3 = new aws.S3(params);
exports.process = function(bucketName, upload, zipName) {
var cb = uploadFiles(upload, zipName);
createBucket(bucketName, cb);
listAllBuckets();
};
function createBucket(bucketName, cb) {
params.Bucket = bucketName;
s3.createBucket({Bucket: bucketName}, function(err, data) {
if (err) {
console.log('Error creating bucket ' + '[' + bucketName + ']' + ': ' + err);
} else {
var bucketCreated = data.Location.replace(/^\/+/, "");
console.log('Bucket created: ' + bucketCreated);
cb();
}
});
}
function uploadFiles(upload, fileName) {
return function() {
params.Body = upload;
params.Key = fileName + '.zip';
s3.upload(params)
.on('httpUploadProgress', function(evt) {
console.log(evt);})
.send(function(err, data) {
if(err) {
console.log('Upload error: ' + err);
}
console.log('Upload data: ' + data);
});
};
}
function listAllBuckets() {
s3.listBuckets(function(err, data) {
if (err) {
console.log('Error: ' + err);
} else {
for (var i in data.Buckets) {
var bucket = data.Buckets[i];
console.log('Bucket ' + bucket.Name + ':' + bucket.CreationDate);
}
}
});
}