Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs uploading to s3 using knox?

for example:

knox.js:

knox.putFile("local.jpeg", "upload.jpeg", {
          "Content-Type": "image/jpeg"
        }, function(err, result) {
          if (err != null) {
            return console.log(err);
          } else {
            return console.log("Uploaded to amazon S3");

I have two images in the same directory as knox.js, local.jpeg and local2.jpeg, i am able to upload local.jpeg to s3, but not local2.jpeg, both files have the same permissions. am i missing anything here? thanks

like image 631
unknown Avatar asked Nov 10 '12 14:11

unknown


1 Answers

My implementation without store in locale. With express, knox, mime, fs.

var knox = require('knox').createClient({
    key: S3_KEY,
    secret: S3_SECRET,
    bucket: S3_BUCKET
});

exports.upload = function uploadToAmazon(req, res, next) {
    var file = req.files.file;
    var stream = fs.createReadStream(file.path)
    var mimetype = mime.lookup(file.path);
    var req;

    if (mimetype.localeCompare('image/jpeg')
        || mimetype.localeCompare('image/pjpeg')
        || mimetype.localeCompare('image/png')
        || mimetype.localeCompare('image/gif')) {

        req = knox.putStream(stream, file.name,
            {
                'Content-Type': mimetype,
                'Cache-Control': 'max-age=604800',
                'x-amz-acl': 'public-read',
                'Content-Length': file.size
            },
            function(err, result) {
                console.log(result);
            }
       );
       } else {
        next(new HttpError(HTTPStatus.BAD_REQUEST))
       }

       req.on('response', function(res){
           if (res.statusCode == HTTPStatus.OK) {
               res.json('url: ' + req.url)
           } else {
               next(new HttpError(res.statusCode))
           }
});
like image 154
Rinat Mukhamedgaliev Avatar answered Nov 13 '22 22:11

Rinat Mukhamedgaliev