I have an angular 2 application that is sending an image wrapped inside a FormData object to my node server.
I can receive the image on the server and write it to a file, then redownload it:
var busboy = new Busboy({ headers: req.headers });
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
if(mimetype == "image/png"){
var saveTo = path.join('/home/ec2-user/bonappetite/tmp', path.basename(filename) + ".png");
} else if(mimetype == "image/jpeg") {
var saveTo = path.join('/home/ec2-user/bonappetite/tmp', path.basename(filename) + ".jpg");
} else {
var saveTo = path.join('/home/ec2-user/bonappetite/tmp', path.basename(filename));
}
file.pipe(fs.createWriteStream(saveTo));
});
busboy.on('finish', function() {
console.log("url retrieved");
res.writeHead(200, { 'Connection': 'close' });
res.end(savedUrl);
});
return req.pipe(busboy);
What I am trying to do is take the image inside the FormData and upload it to my S3 bucket. I am able to upload data myself, but I cant seem to upload the image that I have saved from the post request
var AWS = require('aws-sdk');
AWS.config.loadFromPath('./aws_config.json');
var s3Bucket = new AWS.S3( { params: {Bucket: 'xxxx'} } );
params = {Key: filename, Body: file};
s3Bucket.putObject(params, function(err, data) {
if (err) {
console.log(err)
} else {
var urlParams = {Bucket: 'xxxx', Key: filename};
s3Bucket.getSignedUrl('getObject', urlParams, function(err, url){
if(err){
console.log(err);
res.status( err.status || 500 );
res.send( error );
}else {
res.setHeader('Content-Type', 'application/json');
url = url;
res.send({"url":url});
}
})
}
});
How can I take the file that I have just uploaded to my server and stream it to S3?
Do i even need to save the file to disk prior to sending it to s3?
Thanks