Skip to content

Commit a909a0c

Browse files
authored
feat(minio): Allow for proxies (#1184)
* feat(minio): Allow for proxies * fix: Declared proxy_client as None * refactor(proxy): Change to `str | None` with "auto"
1 parent f37ebbe commit a909a0c

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

openml/_api_calls.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import urllib.parse
1111
import xml
1212
import xmltodict
13+
from urllib3 import ProxyManager
1314
from typing import Dict, Optional, Union
1415

1516
import minio
@@ -23,6 +24,26 @@
2324
)
2425

2526

27+
def resolve_env_proxies(url: str) -> Optional[str]:
28+
"""Attempt to find a suitable proxy for this url.
29+
30+
Relies on ``requests`` internals to remain consistent. To disable this from the
31+
environment, please set the enviornment varialbe ``no_proxy="*"``.
32+
33+
Parameters
34+
----------
35+
url : str
36+
The url endpoint
37+
38+
Returns
39+
-------
40+
Optional[str]
41+
The proxy url if found, else None
42+
"""
43+
resolved_proxies = requests.utils.get_environ_proxies(url)
44+
selected_proxy = requests.utils.select_proxy(url, resolved_proxies)
45+
return selected_proxy
46+
2647
def _create_url_from_endpoint(endpoint: str) -> str:
2748
url = config.server
2849
if not url.endswith("/"):
@@ -84,6 +105,7 @@ def _download_minio_file(
84105
source: str,
85106
destination: Union[str, pathlib.Path],
86107
exists_ok: bool = True,
108+
proxy: Optional[str] = "auto",
87109
) -> None:
88110
"""Download file ``source`` from a MinIO Bucket and store it at ``destination``.
89111
@@ -95,7 +117,10 @@ def _download_minio_file(
95117
Path to store the file to, if a directory is provided the original filename is used.
96118
exists_ok : bool, optional (default=True)
97119
If False, raise FileExists if a file already exists in ``destination``.
98-
120+
proxy: str, optional (default = "auto")
121+
The proxy server to use. By default it's "auto" which uses ``requests`` to
122+
automatically find the proxy to use. Pass None or the environment variable
123+
``no_proxy="*"`` to disable proxies.
99124
"""
100125
destination = pathlib.Path(destination)
101126
parsed_url = urllib.parse.urlparse(source)
@@ -107,7 +132,16 @@ def _download_minio_file(
107132
if destination.is_file() and not exists_ok:
108133
raise FileExistsError(f"File already exists in {destination}.")
109134

110-
client = minio.Minio(endpoint=parsed_url.netloc, secure=False)
135+
if proxy == "auto":
136+
proxy = resolve_env_proxies(parsed_url.geturl())
137+
138+
proxy_client = ProxyManager(proxy) if proxy else None
139+
140+
client = minio.Minio(
141+
endpoint=parsed_url.netloc,
142+
secure=False,
143+
http_client=proxy_client
144+
)
111145

112146
try:
113147
client.fget_object(

0 commit comments

Comments
 (0)