Solution for Catch exceptions when uploading files to google-cloud-storage bucket using python
is Given Below:
I’m using the function below to upload local files to my google storage bucket. What is the proper way of catching all possible errors? I would like to use all and the most specific exceptions that might occur while attempting an upload.
How would I approach finding all the possible errors that might occur and implementing them?
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)
If you want to catch all exceptions, a simply try-except block will do it.
try: blob = bucket.blob(destination_blob_name) blob.upload_from_filename(source_file_name) except Exception as e: print(e)
Now, if you want to have specific blocks for particular exceptions you might need to look at the GCS API errors list: https://cloud.google.com/storage/docs/xml-api/reference-status#standardcodes