【问题标题】:React Native + Amplify S3 ExampleReact Native + Amplify S3 示例
【发布时间】:2018-09-29 14:22:06
【问题描述】:

我正在寻找一个完整的示例,说明如何在成功将文件名发送到S3 后返回文件名。

目前我有类似的东西:

uploadImageAsync = async (pickerResult) => {
    try {
      this.setState({
        uploading: true,
      })

      if (!pickerResult.cancelled) {
        const response = await fetch(pickerResult.uri);
        const blob = await response.blob();
        Storage.put(fileName, blob)
          .then(result => console.log(result))
          .catch(err => console.log(err))
      }
    } finally {
      this.setState({
        uploading: false,
      })
    }
  }

我的返回结果是我创建的文件名。我想在创建后返回 S3 文件名。这可能吗?

【问题讨论】:

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


    【解决方案1】:

    您可以对图像执行get() 调用以获取签名的网址。

    Storage.put('profile-picture.jpeg', blob)
      .then(result => {
        Storage.get('profile-picture.jpeg')
          .then(result => {
            console.log(result);                    
          })
          .catch(err => {
            console.error(err);
          });
      })
      .catch(err => {
        console.error(err);
      });
    

    【讨论】:

    • 这就是我最终做的!非常感谢。我认为现在向用户展示它是我的下一个挑战——我看到了一些看起来很有帮助的项目,但经过测试,并不理想......如果你对此有任何建议,我很乐意听到 - 因为这些东西太新了,文档变得非常混乱 - 并且没有达到明显的要点。
    • 要在react-native 中显示图像,您可以使用内置的<Image source={{uri: 'some-uri'}} /> 或类似react-native-scalable-image 的东西。第一个解决方案应该有效,因为您的 url 已签名,因为您使用 S3 sdk 检索了它。这能回答你的问题吗?
    【解决方案2】:

    因此,您实际上不需要在执行 put 后​​将 S3 文件的密钥返回给您,因为它始终具有基于您提供给“put”的第三个参数的可预测密钥。

    • 公共级别/省略第三个参数:“public/yourFileName”
    • 受保护级别/{level: 'protected'}: "protected/IdentityId/fileName"
    • 私有级别/{level: 'private'}: "private/IdentityId/fileName"

    put 函数会自动为您的文件名组合前缀。但是,如果出于某种原因需要它,您可以像这样获得“IdentityId”部分:

    Auth.currentCredentials().then(user => user.identityId)
    

    【讨论】:

    • 谢谢!很高兴知道。 :)
    猜你喜欢
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 2020-08-21
    • 2015-05-31
    • 1970-01-01
    相关资源
    最近更新 更多