![](/img/trans.png)
[英]How to upload a base64 encoding string as an image to S3 using AWS Lambda [NodeJS]
[英]base64 image corrupted uploading to S3
router.post('/image', multipartMiddleware , function(req, res) {
var file_name = req.body.name;
var data = req.body.data;
return s3fsImpl.writeFile(file_name , data , 'base64').then(function (err) {
res.status(200).end();
});
});
我上面的代碼有什么問題? 我的 therminal 沒有錯誤,我的 s3 中有該文件,但是當我下載它時它已損壞。
由於我不知道您的代碼中的s3fsImpl
是什么,因此我無法針對您的實現回答這個問題,但這是我使用 aws-sdk 的方式:
const AWS = require('aws-sdk');
const s3 = new AWS.S3({apiVersion: '2006-03-01'});
const file_name = req.body.name;
const data = req.body.data;
// We need to get the format of the file from the base64 string i.e. data:image/png;base64<base64String>
const format = data.substring(data.indexOf('data:')+5, data.indexOf(';base64'));
// We need to get the actual file data from the string without the base64 prefix
const base64String = data.replace(/^data:image\/\w+;base64,/, '');
const buff = new Buffer(base64String,'base64');
s3.upload({
Bucket:'s3-bucket-name',
Key: file_name,
Body: buff,
ContentEncoding:'base64',
ContentType:format
}, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.