使用ImagePicker和s3在React本机中上传文件

时间:2019-06-05 23:46:08

标签: amazon-web-services react-native amazon-s3 react-native-image-picker

我正在尝试将图像从设备直接上传到s3。我能够读取图像元数据并将其发送到服务器以生成aws s3的预签名URL。我也有预签名的URL,我想使用axios上传文件/图像,但是不知何故图像/文件没有被上传。这是我的代码。

图像数据(由ImagePicker读取)

data: "" // image raw data
fileName: "acx.jpg"
fileSize: ""
uri: ""
path: ""

用于将所选图像发送到AWS s3的代码。

const options = { headers: { 'Content-Type': fileType}};
axios.put(res.data.signedRequest, data , options);

我得到了以下的休止符。

res = {
  config: 
  data: ""
  status: 200
  StatusText: undefined
  ...
}

那我应该在axios请求中作为数据传递什么?

1 个答案:

答案 0 :(得分:1)

您浏览过此plugin吗?这将使过程变得容易得多。然后,您可以尝试

 upload = () => {
  const file = {
    uri: this.state.imageuri,
    name: acx.jpg,
    type: "image/jpeg"
  };
  const options = {
    keyPrefix: "ts/",
    bucket: "celeb-c4u",
    region: "eu-west-1",
    accessKey: "AKIAI2NHLR7A5W2R3OLA",
    secretKey: "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",
    successActionStatus: 201
  };
  return RNS3.put(file, options)
    .then(response => {
      if (response.status !== 201)
        throw new Error("Failed to upload image to S3");
      else {
        console.log(
          "Successfully uploaded image to s3. s3 bucket url: ",
          response.body.postResponse.location
        );
        this.setState({
          url: response.body.postResponse.location
        });
      }
    })
    .catch(error => {
      console.log(error);
    });
};