-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS3DataUpload_AWS.js
99 lines (90 loc) · 2.48 KB
/
S3DataUpload_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use-strict';
var schedule = require('node-schedule');
var fs = require('fs');
var aws = require('aws-sdk');
var async = require('async');
//var Buffer = require('buffer');
var path = require("path");
aws.config.update({
accessKeyId: "accesskey",
secretAccessKey: "secretAccessKey",
region : "eu-west-1"
});
var s3Bucket = new aws.S3( {
params: {
Bucket: "bucketname"
}
} );
var count=1;
var directoryName = '../foldername';
var distFolderPath = path.join(__dirname,directoryName);
var dt = new Date();
var utcDate = dt.toUTCString();
console.log("starting time is ",utcDate);
console.log(distFolderPath);
//var j = schedule.scheduleJob('0 0 */1 * * * ', function(){ one second
var j = schedule.scheduleJob('*/1 * * * * ', function(){
var dt = new Date();
var utcDate = dt.toUTCString();
console.log("count is ",count++,utcDate);
params1= {Bucket: "bucketname"};
var filenamelist = [];
var promise1 = new Promise(function (resolve, reject){
s3Bucket.listObjects(params1, function(err, data){
//console.log(data);
var bucketContents = data.Contents;
async.forEach(bucketContents, function (item, callback){
//console.log(item.Key);
filenamelist.push(item.Key);
}, function(err) {
console.log('iterating done');
});
//console.log('length :',filenamelist.length)
resolve(filenamelist);
});
});
promise1.then(function (value){
//fulfillment
console.log('length of array is',filenamelist.length);
fs.readdir(distFolderPath, (err, files) => {
if(!files || files.length === 0) {
console.log(`provided folder '${distFolderPath}' is empty or does not exist.`);
return;
}
for (const fileName of files) {
var filePath = path.join(distFolderPath, fileName);
//console.log(filenamelist[0]);
//console.log(filePath);
//console.log(distFolderPath);
var flag=0;
for(var i=0;i<filenamelist.length;i++)
{
// console.log(filenamelist[i]);
// console.log(fileName);
if(fileName == filenamelist[i])
{
flag=1;
console.log("image is same");
break;
}
}
if(flag==0){
console.log(fileName);
fs.readFile(filePath, (error, fileContent) => {
var params = {
Key: fileName,
Body: fileContent,
ContentEncoding: 'data:image',
ContentType: 'image/*',
ACL: 'public-read'
};
s3Bucket.putObject(params, function (err, results) {
if (err) console.error(err);
//console.log(results);
});
});
}
}
});
});
});