Javascript nodejs使用knox上传到s3?

Javascript nodejs使用knox上传到s3?,javascript,node.js,Javascript,Node.js,例如: 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("Up

例如:

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");

我有两个图像与knox.js在同一目录下,local.jpeg和local2.jpeg,我可以将local.jpeg上传到s3,但不能上传local2.jpeg,这两个文件具有相同的权限。我有什么遗漏吗?谢谢,那是因为你的代码没有上传local2.jpeg

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


我的实现没有在区域设置中存储。使用
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))
           }
});

如何指定s3 buckets3的文件夹没有确切的“文件夹”。您只需在文件前面加上“foo/bar/”等,s3控制台就会显示它,就好像它在文件夹中一样。在本例中,将参数putStream从file.name更改为“foo/”+file.name就可以了。
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))
           }
});