node.js - 使用 NodeJS 和 S3FS/multiparty 将文件上传到 Amazon S3

标签 node.js amazon-web-services amazon-s3 amazon-cloudfront

我正在使用多方和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/

相关文章:

java - 根据对象键的子字符串查找/删除S3对象

node.js - 如何用mongoose实现分页和总计

node.js - async/await 不适用于 mongoose 实例方法

amazon-web-services - 如何将Spark EMR集群与AWS Elasticsearch集群连接

amazon-web-services - IntelliJ:设置Gradle运行配置的AWS环境变量

amazon-web-services - 列出具有特定存储类的 S3 存储桶对象

python - aws s3 by boto3 上传文件的 key 添加了一些奇怪的字符串

node.js - SNS confirmSubscription 使用 NodeJs 和 Express

node.js - 如何在nodejs应用程序中输出mongodb集合以获取它们的响应

amazon-web-services - AWS AutoScaling - 实例生命周期