Ayu*_*pta 1 file-upload amazon-s3 node.js aws-sdk aws-lambda
我的Lambda在请求正文(event.body
)中从我的用户接收图像的二进制数据。
我尝试将其正确上传到S3,但是下载时,图像已损坏/无法打开。
我还需要将上传图像的URl返回给用户。
请帮忙!
module.exports.uploadImage = (event, context, callback) => {
var buf = new Buffer(new Buffer(event.body).toString('base64').replace(/^data:image\/\w+;base64,/, ""),'base64');
var data = {
Key: Date.now()+"",
Body: buf,
ContentEncoding: 'base64',
ContentType: 'image/png',
ACL: 'public-read'
};
s3Bucket.putObject(data, function(err, data){
if (err) {
console.log(err);
console.log('Error uploading data: ', data);
} else {
console.log('succesfully uploaded the image!');
}
callback(null,data);
});
};
Run Code Online (Sandbox Code Playgroud)
您可以将图像作为节点Buffer上载到S3。SDK为您完成转换。
const AWS = require("aws-sdk");
var s3 = new AWS.S3();
module.exports.handler = (event, context, callback) => {
var buf = Buffer.from(event.body.replace(/^data:image\/\w+;base64,/, ""),"base64");
var data = {
Bucket: "sample-bucket",
Key: Date.now()+"",
Body: buf,
ContentType: 'image/png',
ACL: 'public-read'
};
s3.putObject(data, function(err, data){
if (err) {
console.log(err);
console.log('Error uploading data: ', data);
} else {
console.log('succesfully uploaded the image!');
}
callback(null,data);
});
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3428 次 |
最近记录: |