以下是使用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");
});
});
答案 0 :(得分:2)
尝试
s3fsImpl.writeFile(file.fileUpload.originalFilename, stream, {"ContentType":"image/jpg"})