Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse Boto3 200 response for copy_object request

I am new to Python and I'm writing an AWS lambda that copies files from one bucket to another. I am using the Boto3 library and have come across the following in the documentation:

A copy request might return an error when Amazon S3 receives the copy request or while Amazon S3 is copying the files. If the error occurs before the copy operation starts, you receive a standard Amazon S3 error. If the error occurs during the copy operation, the error response is embedded in the 200 OK response. This means that a 200 OK response can contain either a success or an error. Design your application to parse the contents of the response and handle it appropriately. If the copy is successful, you receive a response with information about the copied object.

A successful response looks like this (potential sensitive data replaced with '...') :

Response : {'ResponseMetadata': {'RequestId': 'ID....', 'HostId': 'ID...', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amz-id-2': '...', 'x-amz-request-id': '...', 'date': 'Wed,
            10 Feb 2021 21: 48: 48 GMT', 'content-type': 'application/xml', 'content-length': '234', 'server': 'AmazonS3'
        }, 'RetryAttempts': 0
    }, 'CopyObjectResult': {'ETag': '"..."', 'LastModified': datetime.datetime(2021,
        2,
        10,
        21,
        48,
        48, tzinfo=tzlocal())
    }
}

I want to know how I would parse the 200 response to check for errors. The documentation says that if the copy is successful then you will receive a response with information about the copied object. I am guessing that this is the CopyObjectResult section?

I am accessing the response as so:

   response = s3_client.copy_object(CopySource=copy_source_object, Bucket=destination_bucket_name, Key=destination_key_prefix+file_key_name)

I now want to write a statement that says if the object was copied successfully, then delete the object from the source bucket. I will need to check that not only is the response a 200 response but that it also contains no embedded errors. I can't find an example 200 response that includes an error to be sure that I have written it correctly so I am turning to the trusty SO community for help.

Thanks in advance.

like image 780
thomasters Avatar asked Jan 24 '23 11:01

thomasters


1 Answers

I think the docs may be misleading here - they're describing the lower level Amazon APIs, and not the behavior of the higher level client-facing library boto3.

You do not need to parse it and check for errors, boto3/botocore itself will have already parsed the result. If the method returned a value at all, with the embedded CopyObjectResult dict and ETag, then there was no error. The client library will have raised an exception in the case of error.

If you want to handle the possibility of error, then it's a try/except construct around the API call that you'll need.

My reasoning is based around looking in the source, where a post-process hook is registered here:

https://github.com/boto/botocore/blob/1.20.5/botocore/handlers.py#L964-L967

And the implementation is here:

https://github.com/boto/botocore/blob/1.20.5/botocore/handlers.py#L83-L108

It suggests a 500 error in the case of copy_object failure serverside.

like image 159
wim Avatar answered Jan 27 '23 00:01

wim