使用react-native-image-picker将映像从React Native上传到带有预签名URL的Amazon S3

时间:2018-03-16 14:22:50

标签: reactjs react-native amazon-s3 image-upload

我的案子需要帮助。 我正在使用react-native-image-picker来选择图像,当需要上传图像时会出现当前问题。

我准备presignedurl

//here I get the presigned url
  const uploadConfig = await profileApi.getUpdateUrl(id); 
  //I prepare file from response of imagepicker : uri , filename , file type, data(base64 file)
  const file = {
    uri: values.Avatar.uri,
    name: values.Avatar.fileName,
    type: values.Avatar.type,
    data: value.Avatar.data,
  }; 

  //upload to presignedurl of S3 , already tried file.data , file.uri respectively but it resulted same
  const resultUpload = await axios.put(uploadConfig.data.url, file, {
    headers: {
      'Content-Type': file.type,
    },
  });

结果被接受且没有错误,但是它导致了空白图像,当我下载时不是图像。

1 个答案:

答案 0 :(得分:0)

不使用axios而是使用XMLHttpRequest,我不知道为什么,但这有效:

const xhr = new XMLHttpRequest()
xhr.open('PUT', presignedUrl)
xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
        if (xhr.status === 200) {
            console.log('Image successfully uploaded to S3')
        } else {
            console.log('Error while sending the image to S3')
        }
    }
}
xhr.setRequestHeader('Content-Type', response.type)
xhr.send({uri: response.uri, type: response.type, name: response.filename})