通过 gsutil 检查 Google Cloud Storage 存储桶是否存在

问题描述 投票:0回答:2

我正在编写一个函数,通过

gsutil rsync
命令在存储桶之间复制数据。

只是想在通过

rsync
开始
gsutil
操作之前确认源和目标云存储桶是否存在。

是否有现有方法可以通过

cloud storage
检查
gsutil
存储桶是否存在?

google-cloud-platform gcloud gsutil
2个回答
4
投票

目前没有通过

gsutil
检查存储桶是否存在的机制。

参考资料:

https://cloud.google.com/storage/docs/gsutil

https://github.com/GoogleCloudPlatform/gsutil

我们可以这样验证它

  gsutil ls | grep "$BUCKET_NAME"
  test $? -ne 0 && echo “Bucket $BUCKET_NAME doesn't exist, Exiting!!" && exit 1

0
投票

这不是测试存储桶是否存在,而是检查您是否有权访问它。仍然可能足够好

gsutil ls -b "${BUCKET_NAME}"

如果您使用

gsutil ls | ...
,您将列出默认项目中的所有存储桶,因此该列表可能会变得很长,并且如果默认项目不是保存该存储桶的项目,您可能会得到漏报。

© www.soinside.com 2019 - 2024. All rights reserved.