1

我正在尝试将图像从设备直接上传到 s3。我能够读取图像元数据并将其发送到服务器以生成 aws s3 的预签名 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请求中作为数据传递什么?

4

1 回答 1

1

你探索过这个插件 吗?这将使该过程变得容易得多。然后你可以试试

 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);
    });
};
于 2019-06-06T01:44:52.240 回答