图片上传到 s3 不渲染 [英] Image upload to s3 does not render
问题描述
我正在使用 Expo Image Picker 将裁剪后的图像发送到 s3.该文件正在上传,但它不会呈现为图像,因为它未被识别为图像.如果我获取 blob 数据并在 base64 到图像编码器中使用它,我会得到图像,所以它必须是基于 mime 或编码的,这就是我所拥有的.
I am using Expo Image Picker to send a cropped image to s3. The file is being uploaded, but it does not render as an image as its not recognised as one. If I took the blob data and use it in a base64 to image encoder I get the image, so it must be mime or encoding based, here is what I have.
我调用 Expo Image Picker:
I invoke the Expo Image Picker with;
let pickerResult = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [1, 1],
base64: true,
exif: false,
});
我用来从 s3 SDK 创建签名 URL 的参数是;
The params I use to create the signed URL from s3 SDK are;
const params = {
Bucket: 'images,
Key: 'filename.jpg',
Expires: 60 * 5,
ACL: 'public-read',
ContentEncoding: 'base64',
ContentType: 'image/jpeg'
};
上传是用axios完成的,头文件如下;
The upload is done with axios with the following headers;
const config = {
headers: {
'x-amz-acl' : 'public-read',
'Content-Encoding': 'base64',
'Content-Type': 'image/jpeg'
}
};
return await axios.put(`${signedURL}`,
base64data,
config)
.then(response => {
console.log('IMAGE UPLOAD SUCCESS');
return response.data;
})
如果我将创建的 .jpg 文件更改为 .txt 并在 sublime 中查看它,则数据是 base64 编码的字符串,而不是 jpeg 的通常 blob 数据.
If I change the created .jpg file as .txt and view it in sublime the data is the base64 encoded string rather than the usual blob data of a jpeg.
我做错了什么?
推荐答案
好的;找到了解决办法;
Ok; found the solution;
使用缓冲 npm 模块;
use the buffer npm module;
从缓冲区"导入{缓冲区};
import { Buffer } from 'buffer';
将此添加到我的函数中
return await axios.put(`${signedURL}`,
new Buffer(base64data, 'base64'), // make this a buffer
config)
.then(response => {
console.log('IMAGE UPLOAD SUCCESS');
return response.data;
})
这篇关于图片上传到 s3 不渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!