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

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...