【问题标题】:File Upload in React native using ImagePicker and s3使用 ImagePicker 和 s3 在 React Native 中上传文件
【发布时间】:2019-06-05 23:46:08
【问题描述】:

我正在尝试将图像从设备直接上传到 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 请求中作为数据传递什么?

【问题讨论】:

  • 你找到解决方案了吗?

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


【解决方案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);
    });
};

【讨论】:

  • 我知道这个插件。但我想通过签名 URL 上传,因为我不想在前端/移动应用程序中使用 accessKeysecretKey
  • 您可以将它们设置为将在构建过程中配置的环境变量。
猜你喜欢
  • 2020-07-25
  • 2018-11-28
  • 2018-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-05
  • 2020-09-11
  • 2020-03-26
相关资源
最近更新 更多