Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Add an option to upload and download binary files via write_bytes and read_bytes

Vasek Mlejnskyf227b80f29e4
diff --git a/.changeset/silly-moose-wink.md b/.changeset/silly-moose-wink.md
new file mode 100644
index 000000000..6227ec51c
--- /dev/null
+++ b/.changeset/silly-moose-wink.md
@@ -0,0 +1,5 @@
+---
+"@e2b/python-sdk": minor
+---
+
+Add an option to upload and download binary files via `write_bytes` and `read_bytes`
diff --git a/package-lock.json b/package-lock.json
index 640ba1047..8e77ae12e 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -10148,7 +10148,7 @@
},
"packages/js-sdk": {
"name": "@e2b/sdk",
- "version": "0.2.4",
+ "version": "0.2.5",
"license": "MIT",
"dependencies": {
"cross-fetch": "^3.1.5",
@@ -10189,7 +10189,7 @@
},
"packages/python-sdk": {
"name": "@e2b/python-sdk",
- "version": "0.2.8"
+ "version": "0.2.9"
}
},
"dependencies": {
diff --git a/packages/python-sdk/.gitignore b/packages/python-sdk/.gitignore
index e3b447e2d..6bcca26f1 100644
--- a/packages/python-sdk/.gitignore
+++ b/packages/python-sdk/.gitignore
@@ -1,3 +1,5 @@
+tests/assets/video-downloaded.webm
+
### Node template
# Logs
logs
diff --git a/packages/python-sdk/e2b/session/filesystem.py b/packages/python-sdk/e2b/session/filesystem.py
index 074bbdeef..2c3bdb011 100644
--- a/packages/python-sdk/e2b/session/filesystem.py
+++ b/packages/python-sdk/e2b/session/filesystem.py
@@ -1,4 +1,5 @@
import logging
+import base64
from typing import Any, List, Optional
from e2b.constants import TIMEOUT
@@ -29,31 +30,32 @@ class FilesystemManager:
def __init__(self, session: SessionConnection):
self._session = session
- # async def read_bytes(self, path: str) -> bytearray:
- # """
- # Reads the whole content of a file as a byte array.
- # This can be used when you cannot represent the data as an UTF-8 string.
-
- # :param path: path to a file
- # :return: byte array representing the content of a file
- # """
- # result: str = await self.session.call(
- # self.service_name, "readBase64", [path]
- # )
- # return bytearray(result, "base64")
-
- # async def write_bytes(self, path: str, content: bytearray) -> None:
- # """
- # Writes content to a file as a byte array.
- # This can be used when you cannot represent the data as an UTF-8 string.
-
- # :param path: path to a file
- # :param content: byte array representing the content to write
- # """
- # base64_content = base64.b64encode(content).decode("utf-8")
- # await self.session.call(
- # self.service_name, "writeBase64", [path, base64_content]
- # )
+ async def read_bytes(self, path: str) -> bytearray:
+ """
+ Reads the whole content of a file as a byte array.
+ This can be used when you cannot represent the data as an UTF-8 string.
+
+ :param path: path to a file
+ :return: byte array representing the content of a file
+ """
+ result: str = await self._session._call(
+ self._service_name, "readBase64", [path]
+ )
+ return base64.b64decode(result)
+ # return bytearray(result, "base64")
+
+ async def write_bytes(self, path: str, content: bytearray) -> None:
+ """
+ Writes content to a file as a byte array.
+ This can be used when you cannot represent the data as an UTF-8 string.
+
+ :param path: path to a file
+ :param content: byte array representing the content to write
+ """
+ base64_content = base64.b64encode(content).decode("utf-8")
+ await self._session._call(
+ self._service_name, "writeBase64", [path, base64_content]
+ )
async def read(self, path: str, timeout: Optional[float] = TIMEOUT) -> str:
"""
diff --git a/packages/python-sdk/example.py b/packages/python-sdk/example.py
index 9aba9ad27..4baf17c65 100644
--- a/packages/python-sdk/example.py
+++ b/packages/python-sdk/example.py
@@ -12,15 +12,9 @@ async def main():
print("a")
s = Session("Nodejs")
await s.open()
- print("b")
- proc = await s.process.start(
- cmd="echo testing",
- on_stderr=lambda data: print(data),
- on_stdout=lambda data: print(data),
- on_exit=lambda: print("exit"),
- )
- await proc
+
+
await s.close()
return
diff --git a/packages/python-sdk/results.csv b/packages/python-sdk/results.csv
new file mode 100644
index 000000000..d1451ee6b
--- /dev/null
+++ b/packages/python-sdk/results.csv
@@ -0,0 +1,16 @@
+test_name,duration,outcome
+tests/test_filesystem.py::test_list_files,1.295,passed
+tests/test_filesystem.py::test_create_file,2.5103,passed
+tests/test_filesystem.py::test_read_and_write,2.1167,passed
+tests/test_filesystem.py::test_list_delete_files,2.5485,passed
+tests/test_filesystem.py::test_watch_dir,3.8996,passed
+tests/test_filesystem.py::test_write_bytes,1.3645,passed
+tests/test_filesystem.py::test_read_bytes,2.6571,passed
+tests/test_process.py::test_process_on_stdout_stderr,2.3083,passed
+tests/test_process.py::test_process_on_exit,2.358,passed
+tests/test_process.py::test_process_send_stdin,3.1788,passed
+tests/test_session.py::test_create_session,1.5866,passed
+tests/test_session.py::test_create_multiple_sessions,4.0752,passed
+tests/test_timeout.py::test_create_session_timeout,0.0138,passed
+tests/test_timeout.py::test_process_timeout,2.0876,passed
+tests/test_timeout.py::test_filesystem_timeout,2.8059,passed
diff --git a/packages/python-sdk/tests/assets/video.webm b/packages/python-sdk/tests/assets/video.webm
new file mode 100644
index 000000000..77f488a57
Binary files /dev/null and b/packages/python-sdk/tests/assets/video.webm differ
diff --git a/packages/python-sdk/tests/test_filesystem.py b/packages/python-sdk/tests/test_filesystem.py
index 5fe36825d..7b49670eb 100644
--- a/packages/python-sdk/tests/test_filesystem.py
+++ b/packages/python-sdk/tests/test_filesystem.py
@@ -1,5 +1,6 @@
+import filecmp
from asyncio import sleep
-from os import getenv
+from os import getenv, path
from e2b import Session
@@ -74,3 +75,59 @@ async def test_watch_dir():
assert event["path"] == "/tmp/test.txt"
await session.close()
+
+
+async def test_write_bytes():
+ file_name = "video.webm"
+ local_dir = "tests/assets"
+ remote_dir = "/tmp"
+
+ local_path = path.join(local_dir, file_name)
+ remote_path = path.join(remote_dir, file_name)
+
+ # TODO: This test isn't complete since we can't verify the size of the file inside session.
+ # We don't have any SDK function to get the size of a file inside session.
+
+ session = await Session.create("Nodejs", api_key=E2B_API_KEY)
+
+ # Upload the file
+ with open(local_path, "rb") as f:
+ content = f.read()
+ await session.filesystem.write_bytes(remote_path, content)
+
+ # Check if the file exists inside session
+ files = await session.filesystem.list(remote_dir)
+ assert file_name in [x.name for x in files]
+
+ await session.close()
+
+async def test_read_bytes():
+ file_name = "video.webm"
+ local_dir = "tests/assets"
+ remote_dir = "/tmp"
+
+ local_path = path.join(local_dir, file_name)
+ remote_path = path.join(remote_dir, file_name)
+
+ # TODO: This test isn't complete since we can't verify the size of the file inside session.
+ # We don't have any SDK function to get the size of a file inside session.
+
+ session = await Session.create("Nodejs", api_key=E2B_API_KEY)
+
+ # Upload the file first
+ with open(local_path, "rb") as f:
+ content = f.read()
+ await session.filesystem.write_bytes(remote_path, content)
+
+ # Download the file
+ content = await session.filesystem.read_bytes(remote_path)
+
+ # Save the file
+ downloaded_path = path.join(local_dir, "video-downloaded.webm")
+ with open(downloaded_path, "wb") as f:
+ f.write(content)
+
+ # Compare if both files are equal
+ assert filecmp.cmp(local_path, downloaded_path)
+
+ await session.close()