节点js s3fs图像上传到aws amazon s3服务器并设置ContentType

时间:2016-01-05 18:14:34

标签: node.js amazon-s3

以下是使用s3fs库将图像上传到Amazon S3的节点javascript代码。图片正在上传,但当我尝试在浏览器中查看该图片时,它会给出下面提到的错误。

我检查了内容类型,默认情况下它被保存为octet-stream。如果我手动将其设置为image/jpeg,则该图像可在浏览器中查看。任何人都可以告诉我如何在上传时或在上传完成后设置内容类型?

我尝试设置内容类型但不适用于S3fs图片上传。

错误代码: -

<Error>
<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>
</Error>

我的实际代码: -

    var AWS= require('aws-sdk');

    var fs = require('fs');

    var S3FS = require('s3fs');

    var s3fsImpl = new S3FS('bbqmobileimages/images/banners',{
        accessKeyId:'A********A',
        secretAccessKey:'****************'
    });

    var multiparty = require('connect-multiparty'),
        multipartyMiddleware = multiparty();

    router.use(multipartyMiddleware);

        router.post('/api/v1/backoffice/upload/image',function(req,res){

            console.log(JSON.stringify(req.files));
            var file = req.files;
            console.log("file",JSON.stringify(file))
            console.log("original name:- "+file.fileUpload.originalFilename);
            console.log("Path:- ",file.fileUpload.path);
            var stream = fs.createReadStream(file.fileUpload.path);
            return s3fsImpl.writeFile(file.fileUpload.originalFilename, stream).then(function(){
                fs.unlink(file.fileUpload.path, function(err){
                    console.error(err);
                })
                console.log("Sucessfully uploaded to Amazon S3 server");
            });
        });

1 个答案:

答案 0 :(得分:2)

尝试

s3fsImpl.writeFile(file.fileUpload.originalFilename, stream, {"ContentType":"image/jpg"})

在此处找到:https://github.com/RiptideElements/s3fs/issues/45