Bhu*_*oel 10 amazon-s3 amazon-web-services node.js
我想将图像上传到亚马逊网络服务器,为此我使用带有nodejs的aws-sdk.
我能够将图像上传到s3存储桶,但是当我点击URL访问它们时,我得到访问被拒绝错误.
这是我的配置
var AWS = require('aws-sdk');
//aws credentials
AWS.config = new AWS.Config();
AWS.config.accessKeyId = "<access_key>";
AWS.config.secretAccessKey = "<secret_key>";
AWS.config.region = "ap-southeast-1";
AWS.config.apiVersions = {
"s3": "2006-03-01"
}
var s3 = new AWS.S3();
var bodystream = fs.createReadStream(req.files.pic.path);
var params = {
'Bucket': '<bucket_name>',
'Key': 'uploads/images/' + req.files.pic.name,
'Body': bodystream,
'ContentEncoding': 'base64',
'ContentType ': 'image/jpeg'
};
//also tried with s3.putObject
s3.upload(params, function(err, data){
console.log('after s3 upload====', err, data);
})
Run Code Online (Sandbox Code Playgroud)
图像上传成功但其内容类型为application/octet.另外我认为存在一些权限问题,因为当我添加新权限时,我可以下载图像但无法看到它.
你能告诉我这个配置有什么问题吗?我想知道s3.upload和s3.putObject方法之间的区别.
为了指定Content-Type标题,您需要定义Metadata部分:
var params = {
'Bucket': '<bucket_name>',
'Key': 'uploads/images/' + req.files.pic.name,
'Body': bodystream,
'ContentEncoding': 'base64',
Metadata: {
'Content-Type': 'image/jpeg'
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6365 次 |
| 最近记录: |