【问题标题】:How do i upload an image/video to S3 after picking it?选择后如何将图像/视频上传到 S3?
【发布时间】:2019-01-30 21:00:26
【问题描述】:

我正在使用 react native 来构建 iOS 应用。通过使用react-native-image-picker,我设法在我的手机模拟器上获得了图像的uri。我不知道如何将其上传到 Amazon S3(使用 aws Amplify 中的“存储”)。

 ImagePicker.launchImageLibrary(options, (response) => {
      console.log('Response = ', response);

       this.setState({ 
          imFileName: response.fileName,
          fileData: response.data,
          fileURL: response.uri 
        });

       this.putFileInS3();
   }

putFileInS3 = async () => 
{

  Storage.put(this.imFileName, new Buffer(this.fileData, 'base64'), { contentType: 'image/jpeg'})
  .then(() => {
    console.log('successfully saved image to bucket')
  })
  .catch(err => {
  console.log('error saving file to bucket')
})
}

我得到错误:

Possible Unhandled Promise Rejection (id: 0):
TypeError: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type undefined

如何正确调用 Storage.put()?

【问题讨论】:

    标签: reactjs amazon-web-services react-native amazon-s3 aws-amplify


    【解决方案1】:

    您应该使用响应对象的数据字段。

    像这样:

    Storage.put(
          response.fileName,
          new Buffer(response.data, "base64")
    );
    

    你必须安装 Buffer 包并导入它:

    import { Buffer } from "buffer";
    

    您可以在存储模块的文档中阅读更多信息(https://aws-amplify.github.io/docs/js/storage),其中有一个部分是在 React Native 应用程序中上传图像

    希望这会有所帮助。


    更新: launchImageLibrary 应如下所示:

    ImagePicker.launchImageLibrary(options, response => {
      console.log("Response = ", response);
    
      this.setState({
        imFileName: response.fileName,
        fileData: response.data,
        fileURL: response.uri
      });
    
      Storage.put(response.fileName, new Buffer(response.data, "base64"), {
        contentType: "image/jpeg"
      })
        .then(() => {
          console.log("successfully saved image to bucket");
        })
        .catch(err => {
          console.log("error saving file to bucket");
        });
    });
    

    【讨论】:

    • 您好,感谢您的信息。我更新了OP。但这并不太奏效,我不确定为什么(我确实得到了“缓冲区”模块)。我使用的图像选择器也是 react-native-image-picker
    • 我已经更新了答案以包含我使用 launchImageGallery 方法的确切方式。尝试在您的代码中以相同的方式使用它并告诉我它是否有效。
    • 所以事情就是这样,结果 react-native-image-picker 没有返回视频 response.data(仅图像)。它也不能在 iOS 上给你一个 response.path,所以我完全离开了图像选择器。这是一个模块的笑话,希望在另一个 RN 模块上找到更好的支持
    • @chai86 你最后用了什么库?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 2020-10-11
    • 2015-12-20
    • 2020-08-07
    • 1970-01-01
    相关资源
    最近更新 更多