使用NodeJS和S3FS /多方将file upload到Amazon S3

我使用多方和S3FS上传文件到amazon s3,当写入一个文件streams3它创build临时文件path与桶path,例如:

var S3FS = require('s3fs'); var s3fsImpl = new S3FS('my-bucket/files',{ accessKeyId: config.amazonS3.accessKeyId, secretAccessKey: config.amazonS3.secretAccessKey }); module.exports = function (app) { app.post('/upload', function (req, resp) { // get the file location var file = req.files.file; var stream = fs.createReadStream(file.path); return s3fsImpl.writeFile(fileName,stream).then(function(){ fs.unlink(file.path,function(err){ if(err) console.error(err); }); resp.send('done'); }).catch(function (err) { return resp.status(500).send({ message: errorHandler.getErrorMessage(err) }); }); }); }; 

这个文件应该写在s3的path中:

我的桶/文件

而现在它正在写在amazon s3桶中的临时文件path:

 my-bucket/files/home/ubuntu/www/html/sampleProject/public/files 

任何想法为什么临时文件path'home/ubuntu/www/html/sampleProject/public/files'在写入文件时在s3存储桶中创build?

我自己find了解决scheme,我发送的文件名写入文件是错误的,我只取代\/从临时path获取文件名。

 var filePath= 'home\ubuntu\www\html\sampleProject\public\files\myfile.jpg'.replace(/\\/g, '/'); var fileName = filePath.substr(filePath.lastIndexOf('/')+1,filePath.length-1)