文件不存在时,AWS Amplify中Storage.get返回的内容是什么?

时间:2018-06-27 19:28:21

标签: amazon-s3 aws-amplify

我正在尝试使用AWS Ampify,但找不到很好的参考。 A guide,但找不到参考。如果我调用Storage.get(例如下面的代码片段),而test.txt不存在,则返回什么?

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

我发现它返回的网址是404。

2 个答案:

答案 0 :(得分:1)

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

如果要避免使用404,则可以使用Storage.list()检查文件是否存在。或者,您可以尝试在实际使用URL之前预先对其进行一些异常处理。

在我看来,这似乎不是最佳行为,尤其是在使用angular这样的框架时,所以我已经提交了feature request

答案 1 :(得分:0)

在创建新对象之前,我试图找出对象是否存在于存储桶中,这就是我的操作方式,希望对您有所帮助。

//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存储桶权限是公共读取权限。如果您具有不同的存储桶权限,则此解决方案可能不适合您。