简体   繁体   English

如何使用python检查GCS中是否存在桶

[英]How to check whether a bucket exists in GCS with python

Code:代码:

from google.cloud import storage

client = storage.Client()

bucket = ['symbol_wise_nse', 'symbol_wise_final']

for i in bucket:
    if client.get_bucket(i).exists():
        BUCKET = client.get_bucket(i)

if the bucket exists i want to do client.get_bucket.如果存储桶存在,我想做 client.get_bucket。 How to check whether the bucket exists or not?如何检查bucket是否存在?

Another option that doesn't use try: except is:另一个不使用 try: except 的选项是:

from google.cloud import storage

client = storage.Client()    

bucket = ['symbol_wise_nse', 'symbol_wise_final']
for i in bucket:
    BUCKET = client.bucket(i)
    if BUCKET.exists():
        BUCKET = client.get_bucket(i)

There is no method to check if the bucket exists or not, however you will get an error if you try to access a non existent bucket.没有方法可以检查存储桶是否存在,但是如果您尝试访问不存在的存储桶,则会出现错误。

I would recommend you to either list the buckets in the project with storage_client.list_buckets() and then use the response to confirm if the bucket exists in your code, or if you wish to perform the client.get_bucket in every bucket in your project, you can just iterate through the response directly.我建议您使用storage_client.list_buckets()列出项目中的存储桶,然后使用响应来确认您的代码中是否存在存储桶,或者如果您希望在项目中的每个存储桶中执行client.get_bucket ,您可以直接遍历响应。

Hope you find this information useful希望你觉得这些信息有用

You can use something like this:你可以使用这样的东西:

from google.cloud import storage

client = storage.Client()

buckets = ['symbol_wise_nse', 'symbol_wise_final']

for i in buckets:
  try:
       bucket = client.get_bucket(i)
       print(bucket)
  except:
       pass

The following worked for me (re-using params in question):以下对我有用(重新使用有问题的参数):

from google.cloud import storage
from google.cloud.storage import Bucket

client = storage.Client()
exists = Bucket(client, 'symbol_wise_nse').exists()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Gsutil - 如何使用 Gsutil 检查 GCS 存储桶(子目录)中是否存在文件 - Gsutil - How can I check if a file exists in a GCS bucket (a sub-directory) using Gsutil 如何检查Python列表中是否存在元组? - How to check whether a tuple exists in a Python list? 如何检查Python中是否存在方法? - How to check whether a method exists in Python? 如何检查存储桶是否已存在于 python 中的 AWS S3 中 - How to check if bucket already exists in AWS S3 in python Python Firebase 如何查看firebase存储上是否存在文件 - Python Firebase How to check whether a file exists on firebase storage 如何检查列表中的python字典值中是否存在元素? - How to Check whether a element exists in a python dictionary value which is a list? 如何通过python脚本检查用户是否存在于perforce中 - How to check whether user exists in perforce or not through python script python-如何检查netcdf文件中是否存在某些给定的日期 - python - how to check whether some given date exists in netcdf file 如何获取最后一个文件最后一个文件存放在gcs存储桶中(python) - How to get the last file last file deposited in a gcs bucket (python) 如何将 FPDF output 从 python 发送到 GCS 桶? - How to send FPDF output to GCS bucket from python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM