10

例如:

诺克斯.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");

我在与 knox.js 相同的目录中有两个图像,local.jpeg 和 local2.jpeg,我可以将 local.jpeg 上传到 s3,但不能上传 local2.jpeg,这两个文件具有相同的权限。我在这里错过了什么吗?谢谢

4

2 回答 2

12

我的实现没有在语言环境中存储。与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))
           }
});
于 2013-10-31T12:48:53.827 回答
0

那是因为您的代码没有上传 local2.jpeg!

您的代码只会推送名为local.jpeg. 您应该为每个文件调用该knox.put()方法。我还建议您使用一些辅助函数来执行一些字符串格式化以重命名为 s3 上上传的文件(或者保持原样:))

var files = ["local.jpeg", "local1.jpeg"];
for (file in files){
  var upload_name = "upload_"+ file; // or whatever you want it to be called

  knox.putFile(file, upload_name, {
         "Content-Type": "image/jpeg"
     }, function (err, result) {
         if (err != null) {
             return console.log(err);
         } else {
             return console.log("Uploaded to amazon S3");
         }
     });
}
于 2012-11-10T15:59:49.437 回答