【问题标题】: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
【解决方案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 存储桶权限是公共读取访问权限。如果您有不同的存储桶权限,那么此解决方案可能不适合您。