|
| 1 | +# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""Disable endpoint url customizations for s3. |
| 14 | +
|
| 15 | +There's a customization in botocore such that for S3 operations |
| 16 | +we try to fix the S3 endpoint url based on whether a bucket is |
| 17 | +dns compatible. We also try to map the endpoint url to the |
| 18 | +standard S3 region (s3.amazonaws.com). This normally happens |
| 19 | +even if a user provides an --endpoint-url (if the bucket is |
| 20 | +DNS compatible). |
| 21 | +
|
| 22 | +This customization ensures that if a user specifies |
| 23 | +an --endpoint-url, then we turn off the botocore customization |
| 24 | +that messes with endpoint url. |
| 25 | +
|
| 26 | +""" |
| 27 | +from functools import partial |
| 28 | + |
| 29 | +from botocore.handlers import fix_s3_host |
| 30 | + |
| 31 | + |
| 32 | +def register_s3_endpoint(cli): |
| 33 | + handler = partial(on_top_level_args_parsed, event_handler=cli) |
| 34 | + cli.register('top-level-args-parsed', handler) |
| 35 | + |
| 36 | + |
| 37 | +def on_top_level_args_parsed(parsed_args, event_handler, **kwargs): |
| 38 | + # The fix_s3_host has logic to set the endpoint to the |
| 39 | + # standard region endpoint for s3 (s3.amazonaws.com) under |
| 40 | + # certain conditions. We're making sure that if |
| 41 | + # the user provides an --endpoint-url, that entire handler |
| 42 | + # is disabled. |
| 43 | + if parsed_args.command in ['s3', 's3api'] and \ |
| 44 | + parsed_args.endpoint_url is not None: |
| 45 | + event_handler.unregister('before-auth.s3', fix_s3_host) |
0 commit comments