我正在使用多方和S3FS将文件上传到亚马逊s3,当将文件流写入s3时,它会创建临时文件路径以及存储桶路径,例如:
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 中的路径:
我的存储桶/文件
现在它正在亚马逊 s3 存储桶中写入临时文件路径:
my-bucket/files/home/ubuntu/www/html/sampleProject/public/files
知道为什么在写入文件时会在 s3 存储桶内创建临时文件路径 'home/ubuntu/www/html/sampleProject/public/files'
吗?
最佳答案
我自己找到了解决方案,我发送来写入文件的文件名是错误的,我只是在从临时路径获取文件名时将 \
替换为 /
。
var filePath= 'home\ubuntu\www\html\sampleProject\public\files\myfile.jpg'.replace(/\\/g, '/');
var fileName = filePath.substr(filePath.lastIndexOf('/')+1,filePath.length-1)
关于node.js - 使用 NodeJS 和 S3FS/multiparty 将文件上传到 Amazon S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31028187/