微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AWS S3:如何使用bash检查存储桶中是否存在文件

我想知道是否可以检查某个存储桶中是否存在某些文件.

这是我发现的:

Checking if a file is in a S3 bucket using the s3cmd

It should fix my problem,but for some reason it keeps returning that the file doesn’t exist,while it does. This solution is also a little dated and doesn’t use the doesObjectExist method.

Summary of all the methods that can be used in the Amazon S3 web service

This gives the Syntax of how to use this method,but I can’t seem to make it work.

他们是否希望您创建一个布尔变量来保存方法的状态,或者该函数是否直接给出输出/抛出错误

这是我目前在我的bash脚本中使用的代码

existBool=doesObjectExist(${BucketName},backup_${DomainName}_${CurrentDate}.zip)

if $existBool ; then
        echo 'No worries,the file exists.'
fi

我只使用文件名来测试它,而不是给出完整路径.但由于我得到的错误是语法错误,我可能只是错误地使用它.

希望有人可以帮助我,告诉我我做错了什么.

!编辑

我最终寻找另一种方法来做到这一点,因为使用doesObjectExist不是最快或最简单的.

一种简单的方法是使用aws s3 ls
exists=$(aws s3 ls $path_to_file)
if [ -z "$exists" ]; then
  echo "it does not exist"
else
  echo "it exists"
fi

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐