拾取后如何将图像/视频上传到S3?

时间:2019-01-30 21:00:26

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

我正在使用react native来构建iOS应用。我已经通过使用uri在手机模拟器上获得了图像的react-native-image-picker。我不知道如何将其上传到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()?

1 个答案:

答案 0 :(得分:0)

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

赞:

export PYTHONPATH="$PYTHONPATH:/usr/local/lib/python2.7/site-packages:/usr/local/lib/python3.6/site-packages"

您必须安装Buffer软件包并将其导入:

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

可以在存储模块的文档中更多(https://aws-amplify.github.io/docs/js/storage),其中有一个与上传一节中阵营原生应用的图像

希望这会有所帮助。


更新: launchImageLibrary应该是这样的:

import { Buffer } from "buffer";