Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Use dedicated clients for Python template uploads (#1424)

Matt Brockman82d6e323fcfb
diff --git a/.changeset/tall-forks-breathe.md b/.changeset/tall-forks-breathe.md
new file mode 100644
index 000000000..eb94aa804
--- /dev/null
+++ b/.changeset/tall-forks-breathe.md
@@ -0,0 +1,5 @@
+---
+"@e2b/python-sdk": patch
+---
+
+Use dedicated HTTP/1.1 clients for Python template file uploads to signed storage URLs.
diff --git a/packages/python-sdk/e2b/api/__init__.py b/packages/python-sdk/e2b/api/__init__.py
index df732e9ea..60d7359af 100644
--- a/packages/python-sdk/e2b/api/__init__.py
+++ b/packages/python-sdk/e2b/api/__init__.py
@@ -119,6 +119,7 @@ class ApiClient(AuthenticatedClient):
self._async_transport_factory = async_transport_factory
self._thread_local = threading.local()
self._async_clients: Dict[int, httpx.AsyncClient] = {}
+ self._proxy = config.proxy
if require_api_key and require_access_token:
raise AuthenticationException(
diff --git a/packages/python-sdk/e2b/template_async/build_api.py b/packages/python-sdk/e2b/template_async/build_api.py
index 7da04b1d8..5e6278450 100644
--- a/packages/python-sdk/e2b/template_async/build_api.py
+++ b/packages/python-sdk/e2b/template_async/build_api.py
@@ -111,8 +111,14 @@ async def upload_file(
file_name, context_path, ignore_patterns, resolve_symlinks
)
- client = api_client.get_async_httpx_client()
- response = await client.put(url, content=tar_buffer.getvalue())
+ async with httpx.AsyncClient(
+ timeout=api_client._timeout,
+ verify=api_client._verify_ssl,
+ follow_redirects=api_client._follow_redirects,
+ proxy=getattr(api_client, "_proxy", None),
+ http2=False,
+ ) as client:
+ response = await client.put(url, content=tar_buffer.getvalue())
response.raise_for_status()
except httpx.HTTPStatusError as e:
raise FileUploadException(f"Failed to upload file: {e}").with_traceback(
diff --git a/packages/python-sdk/e2b/template_sync/build_api.py b/packages/python-sdk/e2b/template_sync/build_api.py
index 22f0be903..0cba15dfb 100644
--- a/packages/python-sdk/e2b/template_sync/build_api.py
+++ b/packages/python-sdk/e2b/template_sync/build_api.py
@@ -110,8 +110,14 @@ def upload_file(
tar_buffer = tar_file_stream(
file_name, context_path, ignore_patterns, resolve_symlinks
)
- client = api_client.get_httpx_client()
- response = client.put(url, content=tar_buffer.getvalue())
+ with httpx.Client(
+ timeout=api_client._timeout,
+ verify=api_client._verify_ssl,
+ follow_redirects=api_client._follow_redirects,
+ proxy=getattr(api_client, "_proxy", None),
+ http2=False,
+ ) as client:
+ response = client.put(url, content=tar_buffer.getvalue())
response.raise_for_status()
except httpx.HTTPStatusError as e:
raise FileUploadException(f"Failed to upload file: {e}").with_traceback(
diff --git a/packages/python-sdk/tests/async/template_async/test_upload_file.py b/packages/python-sdk/tests/async/template_async/test_upload_file.py
index b50000744..472a4be54 100644
--- a/packages/python-sdk/tests/async/template_async/test_upload_file.py
+++ b/packages/python-sdk/tests/async/template_async/test_upload_file.py
@@ -45,7 +45,12 @@ async def test_upload_file_sets_content_length_and_no_chunked_encoding(tmp_path)
url = f"http://{host}:{port}/upload"
try:
- client = AuthenticatedClient(base_url="http://test", token="test")
+
+ class UploadClient(AuthenticatedClient):
+ def get_async_httpx_client(self):
+ raise AssertionError("signed uploads should not use the API client")
+
+ client = UploadClient(base_url="http://test", token="test")
await upload_file(
api_client=client,
file_name="*.txt",
@@ -69,3 +74,4 @@ async def test_upload_file_sets_content_length_and_no_chunked_encoding(tmp_path)
transfer_encoding = state["headers"].get("Transfer-Encoding")
if transfer_encoding is not None:
assert "chunked" not in transfer_encoding.lower()
+ assert "Authorization" not in state["headers"]
diff --git a/packages/python-sdk/tests/sync/template_sync/test_upload_file.py b/packages/python-sdk/tests/sync/template_sync/test_upload_file.py
index 76b4216d5..609e3178d 100644
--- a/packages/python-sdk/tests/sync/template_sync/test_upload_file.py
+++ b/packages/python-sdk/tests/sync/template_sync/test_upload_file.py
@@ -41,7 +41,12 @@ def test_upload_file_sets_content_length_and_no_chunked_encoding(tmp_path):
url = f"http://{host}:{port}/upload"
try:
- client = AuthenticatedClient(base_url="http://test", token="test")
+
+ class UploadClient(AuthenticatedClient):
+ def get_httpx_client(self):
+ raise AssertionError("signed uploads should not use the API client")
+
+ client = UploadClient(base_url="http://test", token="test")
upload_file(
api_client=client,
file_name="*.txt",
@@ -65,3 +70,4 @@ def test_upload_file_sets_content_length_and_no_chunked_encoding(tmp_path):
transfer_encoding = state["headers"].get("Transfer-Encoding")
if transfer_encoding is not None:
assert "chunked" not in transfer_encoding.lower()
+ assert "Authorization" not in state["headers"]