Search code examples
pythonpython-3.xamazon-web-servicesamazon-s3boto

catch errors if s3 file is not downloaded


I download a file from S3 like this:

s3 = boto3.client('s3')
s3.download_file('testunzipping','DataPump_10000838.zip','/tmp/DataPump_10000838.zip')

For now it always works. However, I wanted to add some sort of error handling. How can I check or get an error message if the download fails. How would I know that there's something wrong?

Does boto3 offer any error handling functions?

I read this: Check if S3 downloading finish successfully but I am looking for alternatives as well.


Solution

  • You can have something like the below. Download and make sure it was created.

    import boto3 
    import os
    
    
    def download_and_verify(Bucket, Key, Filename):
      try:
        os.remove(Filename)
        s3 = boto3.client('s3')
        s3.download_file(Bucket,Key,Filename)
        return os.path.exists(Filename)
      except Exception: # should narrow the scope of the exception
        return False