botocore

Mocking boto3 S3 client method Python

我只是一个虾纸丫 提交于 2019-11-29 20:16:22
I'm trying to mock a singluar method from the boto3 s3 client object to throw an exception. But I need all other methods for this class to work as normal. This is so I can test a singular Exception test when and error occurs performing a upload_part_copy 1st Attempt import boto3 from mock import patch with patch('botocore.client.S3.upload_part_copy', side_effect=Exception('Error Uploading')) as mock: client = boto3.client('s3') # Should return actual result o = client.get_object(Bucket='my-bucket', Key='my-key') # Should return mocked exception e = client.upload_part_copy() However this gives

Mocking boto3 S3 client method Python

北城余情 提交于 2019-11-28 16:25:55
问题 I'm trying to mock a singluar method from the boto3 s3 client object to throw an exception. But I need all other methods for this class to work as normal. This is so I can test a singular Exception test when and error occurs performing a upload_part_copy 1st Attempt import boto3 from mock import patch with patch('botocore.client.S3.upload_part_copy', side_effect=Exception('Error Uploading')) as mock: client = boto3.client('s3') # Should return actual result o = client.get_object(Bucket='my

getting the current user account-id in boto3

社会主义新天地 提交于 2019-11-27 12:47:27
问题 I need to get the account-id of the 'current user' in a boto3 script. Up to now my best solution is to parse the current user arn: >>> import boto3 >>> account_id = boto3.resource('iam').CurrentUser().arn.split(':')[4] but I was wondering if there is a more 'lightweight' approach. In fact >>> timeit.timeit("boto3.resource('iam').CurrentUser().arn", ... 'import boto3', number=10) 4.8895583080002325 and I actually do not need the CurrentUser resource in my script. 回答1: You can get the account