我正在尝试使用nodejs和aws sdk将图像上传到s3。它不断返回奇怪的错误:“无法访问的主机:`images.dynamodb.us-east-1.amazonaws.com”。此服务在'us-east-1'地区可能不可用
这是我的lambda代码:
exports.handler = function(event,context,callback){
var s3 = new AWS.S3();
const image = event.body.imageBinary;
var buf = new Buffer.from(image.replace(/^data:image\/\w+;base64,/, ""),'base64');
const type = image.split(';')[0].split('/')[1];
var params = {
Bucket: process.env.BUCKET,
Key: `${AccountId}.${type}`,
Body: buf,
ContentEncoding: 'base64',
ContentType: `image/${type}`
};
s3.upload(params, function(err, resp){
if (err) {
console.log(err);
} else {
console.log('succesfully uploaded the image!: ' + JSON.stringify(resp));
}
});
}
我什至尝试设置AWS对象配置(包括密钥,私钥和区域),但得到的响应相同
我的aws sdk版本:“ aws-sdk”:“ ^ 2.610.0” 任何帮助都很好
谢谢!
答案 0 :(得分:0)
Lambda支持node.js v12。允许您编写async/await
代码
const AWS = require('aws-sdk');
const s3 = new AWS.S3({
region: 'us-east-1',
apiVersion: '2006-03-01',
});
exports.handler = async(event,context) => {
const image = event.body.imageBinary;
const buf = new Buffer.from(image.replace(/^data:image\/\w+;base64,/, ""),'base64');
const type = image.split(';')[0].split('/')[1];
var params = {
Bucket: process.env.BUCKET,
Key: `${AccountId}.${type}`,
Body: buf,
};
const options = {
ACL: 'private',
CacheControl: 'max-age=86400',
ContentType: `image/${type}`,
ContentEncoding: 'base64',
};
await s3.upload(params, options).promise();
}