如何在 React Native 中直接在 Amazon S3 上上传图像?

wag*_*gng 1 amazon-s3 amazon-web-services react-native

我将使用 React Native 在 Amazon S3 上上传图像。

我有 S3 signedUrl 用于上传图像。

如何在 React Native 中使用此 url 上传图像?

如果有人试过这个,请帮助我。

谢谢!

小智 5

很简单,我花了很多时间才发现:

const xhr = new XMLHttpRequest()
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4) {
    if (xhr.status === 200) {
      // success
    } else {
      // failure
    }
  }
}
xhr.open('PUT', presignedUrl)
xhr.setRequestHeader('Content-Type', fileType)
xhr.send({ uri: filePath, type: fileType, name: fileName })
Run Code Online (Sandbox Code Playgroud)
  • filePath是文件的完整路径
  • fileType是文件的 MIME 类型
  • fileName是文件名

来源:http : //blog.rudikovac.com/react-native-upload-any-file-to-s3-with-a-presigned-url/

  • 我应该指出,预签名的 url 必须已使用 Content-Type 进行签名(如“image/png”等)。直到我们在服务器端也正确签名后,该客户端代码才能正常工作。 (2认同)