我使用下面的函数将本地文件上传到我的google存储桶中。捕获所有可能的错误的正确方法是什么?我想使用在尝试上传时可能发生的所有和最具体的异常。
我将如何找到所有可能发生的错误并实现它们?
from google.cloud import storage
def upload_to_gcp(source_file_name, destination_blob_name):
storage_client = storage.Client()
bucket = storage_client.bucket("some-bucket")
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)发布于 2021-08-02 14:01:40
如果你想捕捉所有的异常,一个简单的try-except代码块就可以做到。
try:
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
except Exception as e:
print(e)现在,如果您想要针对特定异常的特定块,您可能需要查看GCS错误列表:https://cloud.google.com/storage/docs/xml-api/reference-status#standardcodes
https://stackoverflow.com/questions/68620447
复制相似问题