dsaiztc
5/30/2017 - 4:11 PM

boto3 error management

boto3 error management

import boto3
from botocore.exceptions import ClientError as BotoClientError

s3_client = boto3.client('s3')
s3_bucket = 'randombucket'
s3_key = 'randomkey'

try:
  s3_result = s3_client.get_object(Bucket=s3_bucket, Key=s3_key)
except BotoClientError as e:
  error_response = e.response
  
  
# Example of error_response
error_response = {
    'Error': {
        'Code': 'NoSuchKey',
        'Key': 'randomkey',
        'Message': 'The specified key does not exist.'
    },
    'ResponseMetadata': {
        'HTTPHeaders': {
            'content-type': 'application/xml',
            'date': 'Tue, 30 May 2017 15:57:36 GMT',
            'server': 'AmazonS3',
            'transfer-encoding': 'chunked',
            'x-amz-id-2': '<host-id>',
            'x-amz-request-id': '8D3DE39D5B618F56'
        },
        'HTTPStatusCode': 404,
        'HostId': '<host-id>',
        'RequestId': '8D3DE39D5B618F56',
        'RetryAttempts': 0
    }
}