Search code examples
pythonbashamazon-web-servicesamazon-s3s3cmd

Automated backup script via s3cmd and Python -- error handling?


I have a script that executes an s3cmd upload on a schedule. I have yet to encounter any errors in the upload process, but I anticipate I may run into one sometime in the future and would like to send an SNS notification if ever this happens. I know how to send the notification using the Boto3 AWS SDK, however my problem is knowing when to send it.

The script is written in Python, and sends BASH commands for the s3cmd upload using the subprocess module:

>> subprocess.check_output(['bash', '-c', "s3cmd put /path/to/file s3://bucket-name"])

I just want some form of indication when any errors are encountered. What is the best approach to do this?


Solution

  • You should use the python SDK from aws to achieve this: boto3. Then simply add a try/catch block to publish to an SNS if you wish.

    with open('test.jpg', 'rb') as data:
        s3.Bucket('my-bucket').put_object(Key='test.jpg', Body=data)