简体   繁体   English

如何在Node.js中解码Base64字符串并将其作为图像上传到Amazon S3?

[英]How to decode Base64 string and upload as image on to amazon s3 in nodejs?

I tried like this, its producing an image everytime with same name, i want dynamic name and it must be uploaded to amazon s3. 我试图这样,它每次都产生一个具有相同名称的图像,我想要动态名称,并且必须将其上传到Amazon s3。 please help 请帮忙

let base64String = req.body.imgBase64;
      let base64Image = base64String.split(';base64,').pop();
      fs.writeFile('image.jpg', base64Image, { encoding: 'base64' }, function(
        err
      ) {
        console.log('File created');
        res.json('fire created');
      });

This snippet will always name the image 'image.jpg' because you are passing that as a constant into the fs.writeFile() first parameter. 该代码段将始终将图像命名为“ image.jpg”,因为您将其作为常量传递给fs.writeFile()第一个参数。 To dynamically name files, you will need to include a variable within your loop that changes at each iteration. 要动态命名文件,您需要在循环中包含一个变量,该变量在每次迭代时都会更改。 For example, you can do this with an integer: 例如,您可以使用整数:

var i;
var count = 0;
for (i = 0; i < numfiles; i++) {
  let base64String = req.body.imgBase64;
  let base64Image = base64String.split(';base64,').pop();
  var countString = String(count)
  fs.writeFile('image' + countString + '.jpg', base64Image, { encoding: 'base64' }, function(
        err
      ) {
        console.log('File ' + countString + ' created');
        res.json('File ' + countString + ' created');
      });
  count++;
}

As for uploading to S3, there are many sources online to guide you on uploading to S3 using node.js. 至于上传到S3,有许多在线资源可以指导您使用node.js上传到S3。

I would recommend adding a timestamp/datestamp to your image to make it simple. 我建议您在图像中添加时间戳记/日期戳以使其变得简单。 As for the amazon thing, I would recommend asking as a separate question because that's completely different. 至于亚马逊问题,我建议作为一个单独的问题进行询问,因为那是完全不同的。

let base64String = req.body.imgBase64;
      let base64Image = base64String.split(';base64,').pop();
      fs.writeFile(`image${Date.now()}.jpg`, base64Image, { encoding: 'base64' }, function(
        err
      ) {
        console.log('File created');
        res.json('fire created');
      });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过 base64 字符串上传到 Amazon s3 的图像损坏 - Broken image from image upload to Amazon s3 via base64 string 如何在Nodejs中将base64解码为图像? - how to decode base64 to image in Nodejs? 如何使用 nodejs/aws-sdk 将 base64 编码的 pdf 直接上传到 s3? - How to upload base64 encoded pdf directly to s3 with nodejs/aws-sdk? 如何使用节点js将base64数据作为图像上传到s3? - how to upload base64 data as image to s3 using node js? 如何使用NodeJS作为后端从Android上传图像(Base64字符串)到服务器? - How to upload image(Base64 String) to the server from Android with NodeJS as a back end? 如何将十六进制字符串转换为base64以在NodeJS中保存图像 - How to convert a hex string to base64 for image saving in NodeJS 在上传到Amazon s3服务器之前是否有必要将图像从图库转换为base64? - Is it necessary to convert an image from gallery to base64 before uploading to amazon s3 server? 通过 Node.js 将 base64 编码图像上传到 Amazon S3 - Uploading base64 encoded Image to Amazon S3 via Node.js 使用nodejs将base64 pdf文件上传到s3无法在s3打开但能够下载然后打开它工作正常 - upload base64 pdf file to s3 using nodejs unable to open at s3 but able to download and then open it works fine 节点base64上载到AWS S3存储桶会使图像损坏 - Node base64 upload to AWS S3 bucket makes image broken
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM