简体   繁体   English

使用 Node.js 将图像上传到 Amazon S3 会产生一个小的透明方块

[英]Uploading image to Amazon S3 with Node.js results in a small transparent square

I am uploading a image to Amazon S3 bucket but when it arrives there, it's a small transparent square, in the future I will use a Front-end application and the file will be uploaded from user computer.我正在将图像上传到 Amazon S3 存储桶,但是当它到达那里时,它是一个小的透明方块,将来我将使用前端应用程序,文件将从用户计算机上传。

I am using an ReGex for transforming to base64, but when it arrives at the S3 Bucket it is a small square as you can see here:我正在使用 ReGex 转换为 base64,但是当它到达 S3 Bucket 时,它是一个小方块,如下所示:

https://s3.amazonaws.com/node-str-img-bucket/v0u2HHYolOYwXprSeU73v.jpg https://s3.amazonaws.com/node-str-img-bucket/v0u2HHYolOYwXprSeU73v.jpg

My original file URL that I am using for testing upload you can see clicking here .我用于测试上传的原始文件 URL 您可以点击此处查看

Here is my JavaScript uploading process:这是我的 JavaScript 上传过程:

AWS.config.update(config.AWS);
const s3 = new AWS.S3();
const bucket = 'node-str-img-bucket';

let filename = nanoid().toString() + '.jpg';
let rawdata = req.body.image;
let matches = rawdata.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/);
let type = matches[1];
let buffer = new Buffer(matches[2], 'base64');

let params = {
    Bucket: bucket,
    Key: filename,
    Body: rawdata,
    ContentType: type,
    ACL: 'public-read'
};

await s3.upload(params, (error, data) => {
    if (error) {
        console.log(error);
    } else {
        console.log(data);
    }
});

Anything you can help me would be nice.任何你能帮助我的东西都会很好。

use putObject rather than upload使用putObject而不是upload

await s3.putObject(params, (error, data) => {
  if (error) {
    console.log(error);
  } else {
    console.log(data);
  }
});

also change the contentType & Encoding as:还将contentTypeEncoding更改为:

let params = {
  Bucket: bucket,
  Key: filename,
  Body: rawdata,
  ContentEncoding: 'base64',
  ContentType: 'image/jpeg'
  ACL: 'public-read'
};

Had the same issues and tried to solve it for hours.有同样的问题,并试图解决它几个小时。 Seems like the problem is coming from the API Gateway configuration.似乎问题来自 API 网关配置。 If you are using the console,go to the settings and add to the Binary Media Types */* or using open api add x-amazon-apigateway-binary-media-types: [ " / " ]` at the top of the file如果您使用的是控制台,请转到设置并添加到Binary Media Types */* or using open api add x-amazon-apigateway-binary-media-types: [ " / " ]` 在文件的顶部

req.files[0] will have following data req.files[0] 将有以下数据

{ fieldname: 'img', originalname: 'Annotation 2021-01-11 095535.png', encoding: '7bit', mimetype: 'image/png', buffer: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 06 2d 00 00 01 a2 08 06 00 00 00 9b 3e 85 60 00 00 00 01 73 52 47 42 00 ae ce 1c e9 00 00 00 04 ... 38012 more bytes>, size: 38062 } { fieldname: 'img', originalname: 'Annotation 2021-01-11 095535.png', encoding: '7bit', mimetype: 'image/png', buffer: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0D 49 48 44 52 00 00 06 2D 00 00 01 A2 08 06 00 00 00 9B 85 3E 60 00 00 00 01 73 52 47 42 00 AE CE 1C E9 00 00 00 04 ... 38012多个字节>,大小:38062 }

now try this -现在试试这个 -

const { originalname, buffer, encoding, mimetype } = req.files[0]
s3.upload ({Bucket: 'somebucket',
        Key: originalname, 
        Body: buffer,
        ContentEncoding: encoding,
        ContentType: mimetype,
    },function (err, data) {
    if (err) {
       console.log("Error", err);
     } if (data) {
       console.log("Upload Success", data.Location);
     }
   })

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM