-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremoteFileToS3.js
More file actions
41 lines (32 loc) · 1.17 KB
/
remoteFileToS3.js
File metadata and controls
41 lines (32 loc) · 1.17 KB
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
var knox = require('knox');
var request = require('request');
var s3Client = null;
exports.config = function (options) {
s3Client = knox.createClient(options);
};
exports.downloadFromUrlAndUploadToS3 = function (uri, filename, callback) {
if (!s3Client) {
console.error('Error! no config found. Please set s3 configuration using config(options) method before calling downloadFromUrlAndUploadToS3');
}
request.head(uri, function(err, res, body){
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
var s3Request = s3Client.put(filename, {
'Content-Length': res.headers['content-length'],
'Content-Type': res.headers['content-type']
});
var remoteFileRequest = request(uri);
remoteFileRequest.on('data', function (data) {
s3Request.write(data);
}).on('end', function () {
s3Request.end();
});
s3Request.on('response', function(resp) {
console.log('S3 status:', resp.statusCode, 'url:', s3Request.url);
callback(resp);
});
s3Request.on('error', function(err) {
console.error('Error uploading to s3:', err);
});
});
};