-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploadRemote.js
52 lines (41 loc) · 1.48 KB
/
uploadRemote.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
var express = require('express'),
aws = require('aws-sdk'),
bodyParser = require('body-parser');
aws.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});
var app = express(),
s3 = new aws.S3();
app.use(bodyParser.json());
app.post('/upload', function (req, res, next) {
//console.log(req);
//console.log(req.file);
var base64 = req.body.image;
const base64Data = new Buffer(base64.replace(/^data:image\/\w+;base64,/, ""), 'base64');
// Remove header
//var base64Data = base64.split(';base64,').pop();
// Getting the file type, ie: jpeg, png or gif
const type = base64.split(';')[0].split('/')[1];
var time = Date.now();
let imageName = `${time}.${type}`;
const params = {
Bucket: 'signalong-dev', // pass your bucket name
//Key: 'contacts.csv', // file will be saved as testBucket/contacts.csv
Key: imageName, // file will be saved as testBucket/contacts.csv
Body: base64Data,
ContentEncoding: 'base64', // required
ContentType: `image/${type}`
//ACL: 'public-read'
};
s3.upload(params, function(s3Err, data) {
if (s3Err) throw s3Err
console.log(`File uploaded successfully at ${data.Location}`)
res.send("uploaded");
});
//res.send("Uploaded!");
});
app.listen(3001, function () {
console.log(Date.now());
console.log('Example app listening on port 3001!');
});