nodejs使用knox上传到s3?

例如:

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()方法。 我还build议你有一些帮助function,将做一些string格式化重命名为上传的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"); } }); } 

我的实现没有存储在本地。 与expressknoxmimefs

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