boto3 icon indicating copy to clipboard operation
boto3 copied to clipboard

Allow to define client_config when using boto3.setup_default_session

Open Et7f3 opened this issue 6 months ago • 1 comments

Describe the feature

Be able to define default client_config when setting up a default session.

Use Case

I have a code pattern in multiple places, so I had to make a wrapper:

    def create_aws_client(service: str) -> BaseClient:
        return boto3.client(
            service_name=service,
            aws_access_key_id="My AWS_ACCESS_KEY_ID",
            aws_secret_access_key="My AWS_SECRET_ACCESS_ID",
            config=Config(proxies={"https": "My HTTPS_PROXY"})
        )

I see that boto3.setup_default_session can help by setting globally aws_access_key_id and aws_secret_access_key but it doesn't seems to have a parameter for config. https://github.com/boto/boto3/blob/bd077f3741b1bf9e4d9237cfb92a09e61838655e/boto3/init.py#L27-L34 because it forward to Session constructor that just doesn't have such settings: https://github.com/boto/boto3/blob/bd077f3741b1bf9e4d9237cfb92a09e61838655e/boto3/session.py#L49-L57

Proposed Solution

Workaround I use (but would like to avoid)

session = botocore.session.get_session()
session.set_default_client_config(Config(proxies={"https": "My HTTPS_PROXY"}))
boto3.setup_default_session(
  aws_access_key_id="My AWS_ACCESS_KEY_ID",
  aws_secret_access_key="My AWS_SECRET_ACCESS_ID",
  botocore_session=session,
)

Proposed fix:

    def __init__(
        self,
        aws_access_key_id=None,
        aws_secret_access_key=None,
        aws_session_token=None,
        region_name=None,
        botocore_session=None,
        profile_name=None,
+      client_config=None,
    ):
        if botocore_session is not None:
            self._session = botocore_session
        else:
            # Create a new default session
            self._session = botocore.session.get_session()
+          if client_config is not None:
+            self._session.set_default_client_config(client_config)

Other Information

No response

Acknowledgements

  • [X] I may be able to implement this feature request
  • [ ] This feature might incur a breaking change

SDK version used

1.35.2

Environment details (OS name and version, etc.)

not relevant

Et7f3 avatar Aug 21 '24 15:08 Et7f3