base64图像到S3多个图像

时间:2018-09-26 17:17:39

标签: javascript node.js amazon-web-services amazon-s3

我当前正在使用以下代码将base64字符串上传到AWS S3

const promises = images.map(img => s3Bucket.putObject({
      Key: img._id,
      Body: new Buffer(img.replace(/^data:image\/\w+;base64,/, ""),'base64'),
      ContentEncoding: 'base64',
      ContentType: 'image/jpeg',
      ACL: 'public-read'}).promise());

Promise.all(promises)
    .then(data => {
        console.log(data); // this should be an array of resolved s3 put object promises now
        // you probably want to update your product model accordingly here, I'm just copying your existing code
        var url = "https://s3.eu-central-1.amazonaws.com/bucketname/" + String(product._id);
        product.images = url;
        product.save(function(err) {
          if (err) {
            return res.json({
              success: false,
              msg: 'Oops! Something went wrong.' + err
            });
          }
          // Return the product
          return res.json({
            success: true,
            product: product
          });
    })
    .catch(error => {
        //handle the error in case one of the promises gets rejected
        return res.json({
            success: false,
            msg: 'Oops! Something went wrong.' + err
        });
    });
});

过去,这种方法可以很好地将仅一张图像上传到AWS S3平台。现在,我们对应用程序进行了更改,这意味着我们要上传多个图像,因此服务器将收到一组base64图像。将这些文件上传到AWS S3平台的最佳方法是什么?我无法弄清楚。我现在得到的代码返回以下错误:catch(err) { process.nextTick(function() { throw err}); } ^ Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

0 个答案:

没有答案