图像上传到 s3 不呈现

IT技术 reactjs react-native amazon-s3 axios expo
2021-05-10 08:52:20

我正在使用 Expo Image Picker 将裁剪后的图像发送到 s3。该文件正在上传,但它不会呈现为图像,因为它未被识别为图像。如果我使用 blob 数据并在 base64 图像编码器中使用它,我会得到图像,所以它必须是基于 mime 或编码的,这就是我所拥有的。

我调用 Expo Image Picker ;

let pickerResult = await ImagePicker.launchImageLibraryAsync({
  allowsEditing: true,
  aspect: [1, 1],
  base64: true,
  exif: false,
});

我用来从 s3 SDK 创建签名 URL 的参数是;

const params = {
    Bucket: 'images,
    Key: 'filename.jpg',
    Expires: 60 * 5,
    ACL: 'public-read',
    ContentEncoding: 'base64',
    ContentType: 'image/jpeg'
};

上传是使用带有以下标题的 axios 完成的;

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 数据。

我究竟做错了什么?

1个回答

行; 找到了解决办法;

使用缓冲区 npm module;

从“缓冲区”导入{缓冲区};

将此添加到我的功能中

return await axios.put(`${signedURL}`,
      new Buffer(base64data, 'base64'), // make this a buffer
      config)
        .then(response => {
          console.log('IMAGE UPLOAD SUCCESS');
          return response.data;
        })