Google cloud storage 如何使用google云存储api捕获异常

Google cloud storage 如何使用google云存储api捕获异常,google-cloud-storage,jupyter-notebook,google-colaboratory,Google Cloud Storage,Jupyter Notebook,Google Colaboratory,我似乎无法捕捉到这个例外,除非通过“一网打尽” from google.cloud import storage, exceptions def gsutil_ls(bucket_name, filter=None, project_id=None): try: client = storage.Client( project=project_id ) bucket_path = "gs://{}/".format(bucket_name) bucket, err

我似乎无法捕捉到这个例外,除非通过“一网打尽”

from google.cloud import storage, exceptions

def gsutil_ls(bucket_name, filter=None, project_id=None):
  try:
    client = storage.Client( project=project_id )
    bucket_path = "gs://{}/".format(bucket_name)
    bucket, err = client.get_bucket(bucket_name)
    files = ["{}{}".format(bucket_path,f.name) for f in bucket.list_blobs() ]
    if filter:
      files = [f for f in files if filter in f]
    # print(files)
    return files
  except exceptions.NotFound:
    raise ValueError("ERROR: GCS bucket not found, path={}".format(bucket_path))
  except Exception as e:
    print( e)



gsutil_ls("my-bucket", project_id="my-project")
返回:

400获取https://www.googleapis.com/storage/v1/b/my-bucket?projection=noAcl: 无效的存储桶名称:“我的存储桶”

请参阅:

在本例中,了解:

400获取https://www.googleapis.com/storage/v1/b/my-bucket?projection=noAcl: 无效的存储桶名称:“我的存储桶”

使用
异常。禁止使用

  try:
     [...]
  except exceptions.Forbidden:
    raise ValueError("ERROR: GCS bucket not found, path={}".format(bucket_path))

我相信你是在最后一个
中登陆的,除了
子句——如果你也打印
类型(e)
呢?哇<代码>