【问题标题】:Image upload to s3 does not render图片上传到 s3 不渲染
【发布时间】:2018-09-12 10:36:12
【问题描述】:

我正在使用 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 数据。

我做错了什么?

【问题讨论】:

    标签: reactjs react-native amazon-s3 axios expo


    【解决方案1】:

    好的;找到了解决办法;

    使用缓冲 npm 模块;

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

    将此添加到我的函数中

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-27
      • 1970-01-01
      • 2015-08-24
      • 2016-10-07
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      相关资源
      最近更新 更多