|
11 | 11 | from .artifact_storage import AbstractArtifactStorage |
12 | 12 |
|
13 | 13 |
|
| 14 | +def _build_client_kwargs(boto3_client_kwargs): |
| 15 | + """Append the Aim user-agent token to the S3 client's botocore config. |
| 16 | +
|
| 17 | + Any caller-provided endpoint_url, config, and user_agent_extra are kept; the |
| 18 | + aim/<version> token is appended so the client works with any S3-compatible endpoint. |
| 19 | + """ |
| 20 | + import botocore.config |
| 21 | + |
| 22 | + from aim.__version__ import __version__ |
| 23 | + |
| 24 | + kwargs = dict(boto3_client_kwargs) |
| 25 | + config = kwargs.get('config') |
| 26 | + if isinstance(config, dict): |
| 27 | + config = botocore.config.Config(**config) |
| 28 | + aim_user_agent = f'aim/{__version__}' |
| 29 | + if config is not None and config.user_agent_extra: |
| 30 | + aim_user_agent = f'{config.user_agent_extra} {aim_user_agent}' |
| 31 | + user_agent_config = botocore.config.Config(user_agent_extra=aim_user_agent) |
| 32 | + kwargs['config'] = config.merge(user_agent_config) if config is not None else user_agent_config |
| 33 | + return kwargs |
| 34 | + |
| 35 | + |
14 | 36 | class S3ArtifactsStorageAutoClean(AutoClean['S3ArtifactStorage']): |
15 | 37 | def __init__(self, instance: 'S3ArtifactStorage') -> None: |
16 | 38 | super().__init__(instance) |
@@ -69,21 +91,15 @@ def _upload_complete(self, future): |
69 | 91 | def _get_s3_client(self): |
70 | 92 | import boto3 |
71 | 93 |
|
72 | | - client = boto3.client('s3') |
73 | | - return client |
| 94 | + return boto3.client('s3', **_build_client_kwargs({})) |
74 | 95 |
|
75 | 96 |
|
76 | 97 | def S3ArtifactStorage_factory(**boto3_client_kwargs): |
77 | 98 | class S3ArtifactStorageCustom(S3ArtifactStorage): |
78 | 99 | def _get_s3_client(self): |
79 | 100 | import boto3 |
80 | | - import botocore |
81 | 101 |
|
82 | | - if 'config' in boto3_client_kwargs and isinstance(boto3_client_kwargs['config'], dict): |
83 | | - config_kwargs = boto3_client_kwargs.pop('config') |
84 | | - boto3_client_kwargs['config'] = botocore.config.Config(**config_kwargs) |
85 | | - client = boto3.client('s3', **boto3_client_kwargs) |
86 | | - return client |
| 102 | + return boto3.client('s3', **_build_client_kwargs(boto3_client_kwargs)) |
87 | 103 |
|
88 | 104 | return S3ArtifactStorageCustom |
89 | 105 |
|
|
0 commit comments