bash 如何使用 shell 脚本检查存储桶是否存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31077593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use shell script to check if a bucket exists?
提问by toy
I have aws
cli installed. I'm just not sure how to do this in shell script.
我已经aws
安装了 cli。我只是不确定如何在 shell 脚本中执行此操作。
when I run command aws s3 ls s3://bucket
it would give me something like this
当我运行命令时,aws s3 ls s3://bucket
它会给我这样的东西
A client error (NoSuchBucket) occurred when calling the ListObjects operation: The specified bucket does not exist
调用ListObjects操作时出现客户端错误(NoSuchBucket):指定的bucket不存在
That means the bucket doesn't exist. So I want to run that from shell script and check if grep
finds it. But my command doesn't work.
这意味着存储桶不存在。所以我想从shell脚本运行它并检查是否grep
找到它。但是我的命令不起作用。
if [ $(aws s3 ls "s3://$S3_BUCKET" | grep 'NoSuchBucket' &> /dev/null) == 0 ]
then
echo "$S3_BUCKET doesn\'t exist please check again"
exit
fi
It just gave me this
它只是给了我这个
backup.sh: 20: [: 0: unexpected operator
backup.sh: 20: [: 0: 意外的操作符
Updated
更新
I changed the script to be
我将脚本更改为
echo "S3_BUCKET=$S3_BUCKET"
if aws s3 ls "s3://$S3_BUCKET" | grep -q 'AllAccessDisabled'
then
echo "$S3_BUCKET doesn\'t exist please check again"
exit
fi
And this is the output I got
这是我得到的输出
A client error (AllAccessDisabled) occurred when calling the ListObjects operation: All access to this object has been disabled
So the text contains AllAccessDisabled
but I still don't the echo
the next line.
所以文本包含AllAccessDisabled
但我仍然没有echo
下一行。
采纳答案by Etan Reisner
The code you listed wouldn't have given you that error.
您列出的代码不会给您那个错误。
If you had written the script without the space between the leading [
and the $(
that would have.
如果您编写的脚本没有在开头[
和$(
那个之间有空格。
Also grep isn't going to output 0
in that case so that test isn't going to work the way you want.
0
在这种情况下grep 也不会输出,因此测试不会按您想要的方式工作。
If you want to test whether grep
found anything then you want to use the -q
argument to grep
like this:
如果你想测试是否grep
找到任何东西,那么你想使用这样的-q
参数grep
:
if aws s3 ls "s3://$S3_BUCKET" 2>&1 | grep -q 'NoSuchBucket'
then
回答by John Milton
The s3api head-bucket is more direct and does not incur the expense of listing a bucket with many files.
s3api head-bucket 更直接,不会产生列出包含许多文件的bucket 的费用。
http://docs.aws.amazon.com/cli/latest/reference/s3api/head-bucket.html
http://docs.aws.amazon.com/cli/latest/reference/s3api/head-bucket.html
if aws s3api head-bucket --bucket "$S3_BUCKET" 2>/dev/null; then
回答by danelips
Here's how I did it, the other answer will say there is a bucket if there is an AWS error that doesn't contain 'NoSuchBucket' such as token expiry
这是我的做法,如果存在不包含“NoSuchBucket”的 AWS 错误(例如令牌到期),另一个答案会说有一个存储桶
echo "Checking S3 bucket exists..."
BUCKET_EXISTS=true
S3_CHECK=$(aws s3 ls "s3://${BUCKET_NAME}" 2>&1)
#Some sort of error happened with s3 check
if [ $? != 0 ]
then
NO_BUCKET_CHECK=$(echo $S3_CHECK | grep -c 'NoSuchBucket')
if [ $NO_BUCKET_CHECK = 1 ]; then
echo "Bucket does not exist"
BUCKET_EXISTS=false
else
echo "Error checking S3 Bucket"
echo "$S3_CHECK"
exit 1
fi
else
echo "Bucket exists"
fi
回答by Le farfadet
The way recommended by aws is aws s3api head-bucket --bucket $S3_BUCKET
See https://docs.aws.amazon.com/cli/latest/reference/s3api/head-bucket.html
aws 推荐的方式aws s3api head-bucket --bucket $S3_BUCKET
见https://docs.aws.amazon.com/cli/latest/reference/s3api/head-bucket.html
回答by Biplob Biswas
I know this is an old question, but I came here for some answers and using some existing answers and some experimentations of my own came up with a script which handles different return values:
我知道这是一个老问题,但我来到这里寻求一些答案并使用一些现有答案和我自己的一些实验想出了一个处理不同返回值的脚本:
bucketstatus=$(aws s3api head-bucket --bucket "${s3_bucket}" 2>&1)
if echo "${bucketstatus}" | grep 'Not Found';
then
echo "bucket doesn't exist";
elif echo "${bucketstatus}" | grep 'Forbidden';
then
echo "Bucket exists but not owned"
elif echo "${bucketstatus}" | grep 'Bad Request';
then
echo "Bucket name specified is less than 3 or greater than 63 characters"
else
echo "Bucket owned and exists";
fi
回答by Maverick
#!/bin/bash
if [[ -z $(aws s3api head-bucket --bucket my-bucket) ]]; then
echo "bucket exists"
else
echo "bucket does not exist or permission is not there to view it."
fi
回答by vinay
this might me simplest approach
这可能是我最简单的方法
if aws s3 ls "s3://$S3_BUCKET" 2>&1 | grep -q 'An error occurred'
then
echo "bucket does not exit or permission is not there to view it."
else
echo "bucket exit"
fi