Skip to content

Instantly share code, notes, and snippets.

@afuchs
Created January 17, 2014 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afuchs/8475990 to your computer and use it in GitHub Desktop.
Save afuchs/8475990 to your computer and use it in GitHub Desktop.
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