【问题标题】:What is returned by Storage.get in AWS Amplify when the file doesn't exist?当文件不存在时,AWS Amplify 中的 Storage.get 返回什么?
【发布时间】:2018-06-27 19:28:20
【问题描述】:

我正在尝试使用 AWS Ampify,但找不到好的参考。 A guide,我可以找到,但不是参考。如果我调用Storage.get,比如下面的sn-p代码,test.txt不存在,返回什么?

Storage.get('test.txt')
    .then(result => console.log(result))
    .catch(err => console.log(err));

我发现它返回一个导致 404 的 URL。

【问题讨论】:

    标签: amazon-s3 aws-amplify


    【解决方案1】:

    从 Amplify 0.4.7 开始,预期行为是 return a URL that results in a 404

    如果您想避免 404,您可以使用 Storage.list() 检查文件是否存在。或者,您可以尝试在实际使用之前通过一些异常处理预加载 URL。

    这对我来说似乎是次优行为,尤其是对于像 angular 这样的框架,所以我提交了一个feature request

    【讨论】:

      【解决方案2】:

      在创建新对象之前,我试图找出存储桶中是否存在该对象,我就是这样做的,希望对您有所帮助。

      //make a get request of the object you want calling it by it's name
      await Storage.get("key")
            .then((response) => { //The response is a url to the s3 object
              fetch(response).then((result) => { //fetch the URL
                 if(result.status === 200) { //if the file exists
                    console.log("file exists in the bucket");
                 } else { //if the status is 403 or others, the s3 object doesn't exist
                    console.log("file doesnt exist")
                 }
              });
            })
            .catch((err) => console.log(err));
      

      注意:

      我的 S3 存储桶权限是公共读取访问权限。如果您有不同的存储桶权限,那么此解决方案可能不适合您。

      【讨论】:

        猜你喜欢
        • 2021-02-03
        • 1970-01-01
        • 2019-05-06
        • 1970-01-01
        • 1970-01-01
        • 2019-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多