会产生一个小的透明方块

会产生一个小的透明方块

本文介绍了使用 Node.js 将图像上传到 Amazon S3 会产生一个小的透明方块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将图像上传到 Amazon S3 存储桶,但是当它到达那里时,它是一个透明的小方块,将来我将使用前端应用程序并将文件从用户计算机上传.

我正在使用 ReGex 转换为 base64,但是当它到达 S3 Bucket 时,它是一个小方块,正如您在此处看到的:

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

我用于测试上传的原始文件 URL,您可以看到点击此处.>

这是我的 JavaScript 上传过程:

AWS.config.update(config.AWS);const s3 = 新的 AWS.S3();const bucket = 'node-str-img-bucket';让文件名 = nanoid().toString() + '.jpg';让 rawdata = req.body.image;让匹配 = rawdata.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/);让类型 = 匹配 [1];let buffer = new Buffer(matches[2], 'base64');让参数 = {桶:桶,键:文件名,正文:原始数据,内容类型:类型,ACL:'公开阅读'};等待 s3.upload(params, (error, data) => {如果(错误){控制台日志(错误);} 别的 {控制台日志(数据);}});

如果你能帮到我就好了.

解决方案

使用 putObject 而不是 upload

await s3.putObject(params, (error, data) => {如果(错误){控制台日志(错误);} 别的 {控制台日志(数据);}});

还要更改 contentType &编码为:

let params = {桶:桶,键:文件名,正文:原始数据,内容编码:'base64',内容类型:'图像/jpeg'ACL:'公开阅读'};

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.

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:

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.

Here is my JavaScript uploading process:

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

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

also change the contentType & Encoding as:

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

这篇关于使用 Node.js 将图像上传到 Amazon S3 会产生一个小的透明方块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:05