Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var AWS = require('aws-sdk');
var fs = require('fs');
var uuid = require('node-uuid');
var UPLOAD_BUCKET = '';
var AWS_KEY_ID = '';
var AWS_ACCESS_KEY = '';
AWS.config.update({
accessKeyId: AWS_KEY_ID,
secretAccessKey: AWS_ACCESS_KEY
});
var s3 = new AWS.S3();
exports.uploadPhoto = function(req, res){
// Get File upload information
var filename = req.files.image.filename; // actual filename of file
var path = req.files.image.path; //will be put into a temp directory
var mimeType = req.files.image.type; // image/jpeg or actual mime type
fs.readFile(path, function(err, buffer){
var s3Params = {
Bucket: UPLOAD_BUCKET,
Key: getS3Key(),
Body: buffer,
ACL:'public-read',
ContentType: mimeType
};
// Put the Object in the Bucket
s3.client.putObject(s3Params,function(err,data) {
if (err) {
console.log(err);
} else {
console.log("Successfully uploaded data to s3 bucket");
}
res.send({ uploaded: true });
});
});
};
function formatDate(date) {
var d = date.getDate();
var m = date.getMonth() + 1;
var y = date.getFullYear();
return '' + y + (m <= 9 ? '0' + m : m) + (d <= 9 ? '0' + d : d);
}
function getS3Key() {
var key = formatDate(new Date()) + '/' + uuid.v4;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.