Skip to content

Commit 5049319

Browse files
committed
feat: set S3 client user-agent, keep endpoint_url
Signed-off-by: Gonzalo Peña-Castellanos <goanpeca@gmail.com>
1 parent 6e098e3 commit 5049319

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

aim/storage/artifacts/s3_storage.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@
1111
from .artifact_storage import AbstractArtifactStorage
1212

1313

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+
1436
class S3ArtifactsStorageAutoClean(AutoClean['S3ArtifactStorage']):
1537
def __init__(self, instance: 'S3ArtifactStorage') -> None:
1638
super().__init__(instance)
@@ -69,21 +91,15 @@ def _upload_complete(self, future):
6991
def _get_s3_client(self):
7092
import boto3
7193

72-
client = boto3.client('s3')
73-
return client
94+
return boto3.client('s3', **_build_client_kwargs({}))
7495

7596

7697
def S3ArtifactStorage_factory(**boto3_client_kwargs):
7798
class S3ArtifactStorageCustom(S3ArtifactStorage):
7899
def _get_s3_client(self):
79100
import boto3
80-
import botocore
81101

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))
87103

88104
return S3ArtifactStorageCustom
89105

0 commit comments

Comments
 (0)