我在AWS S3中使用node.js上传了一张图片并且已成功上传到AWS S3存储桶,但我尝试查看图片而不是下载。我无法查看下载文件。我使用以下代码在AWS S3上上传图像:
var AWS = require('aws-sdk');
var config = require('../../server/config');
AWS.config.update({
accessKeyId: config.aws.accessKeyId,
secretAccessKey: config.aws.secretAccessKey,
region: config.aws.region
});
var s3 = new AWS.S3();
var Busboy = require('busboy');
var busboyBodyParser = require('busboy-body-parser');
app.use(busboyBodyParser());
app.post('/upload', function(req,res){
var directory = req.body.directory;
console.log(req.files.file);
var image = req.files.file.name;
var contenttype = req.files.file.mimetype;
if(req.body.directory) {
var file = directory+'/'+image;
} else {
var file = image;
}
var data = req.files.file.data;
var keys = {
Bucket: req.body.bucket,
Key: file,
Body: data,
ACL: 'public-read',
contentType: contenttype
};
s3.upload(keys, function(err, result) {
if (err) {
res.send({
isError:true,
status:400,
message:"File Not Uplaod",
data:err
});
} else {
var data = {
Location: result.Location,
key:result.key,
Bucket:result.Bucket
};
res.send({
isError:false,
status:200,
message:"File Uplaod",
data:data
});
}
});
});
答案 0 :(得分:1)
我找到了答案:
使用了ContentType:' image / jpeg'或ContentType:'您的变量'用于在aws s3中上传图像的密钥
答案 1 :(得分:1)
我也一直坚持这一点,但是以下工作有效:
let params = {
ACL: 'public-read',
Bucket: process.env.BUCKET_NAME,
Body: fs.createReadStream(req.file.path),
ContentType: req.file.mimetype,
Key: `avatar/${req.file.originalname}`
};
req.file.mimetype 是它的固定内容,与ContentType: image/jpeg
基本相同,但它标识用户已上传文件的扩展名而不是必须对image/jpeg
或image/png
我希望您的问题已解决。