Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Avoid reading files on upload (#1098)

Mish Ushakov4af9b4c3fc70
diff --git a/.changeset/yellow-plants-matter.md b/.changeset/yellow-plants-matter.md
new file mode 100644
index 000000000..41a89bce3
--- /dev/null
+++ b/.changeset/yellow-plants-matter.md
@@ -0,0 +1,6 @@
+---
+'@e2b/python-sdk': patch
+'e2b': patch
+---
+
+avoid reading files on upload
diff --git a/packages/js-sdk/src/sandbox/filesystem/index.ts b/packages/js-sdk/src/sandbox/filesystem/index.ts
index cf00cc7d7..e57bb8d12 100644
--- a/packages/js-sdk/src/sandbox/filesystem/index.ts
+++ b/packages/js-sdk/src/sandbox/filesystem/index.ts
@@ -32,6 +32,7 @@ import {
ENVD_VERSION_RECURSIVE_WATCH,
} from '../../envd/versions'
import { InvalidArgumentError, TemplateError } from '../../errors'
+import { toBlob } from '../../utils'
/**
* Sandbox filesystem object information.
@@ -354,9 +355,11 @@ export class Filesystem {
if (writeFiles.length === 0) return [] as WriteInfo[]
- const blobs = await Promise.all(
- writeFiles.map((f) => new Response(f.data).blob())
- )
+ const formData = new FormData()
+ for (let i = 0; i < writeFiles.length; i++) {
+ const file = writeFiles[i]
+ formData.append('file', await toBlob(file.data), writeFiles[i].path)
+ }
let user = writeOpts?.user
if (
@@ -373,17 +376,7 @@ export class Filesystem {
username: user,
},
},
- bodySerializer() {
- return blobs.reduce((fd, blob, i) => {
- // Important: RFC 7578, Section 4.2 requires that if a filename is provided,
- // the directory path information must not be used.
- // BUT in our case we need to use the directory path information with a custom
- // multipart part name getter in envd.
- fd.append('file', blob, writeFiles[i].path)
-
- return fd
- }, new FormData())
- },
+ bodySerializer: () => formData,
signal: this.connectionConfig.getSignal(opts?.requestTimeoutMs),
body: {},
})
diff --git a/packages/js-sdk/src/utils.ts b/packages/js-sdk/src/utils.ts
index 8e5af4bc4..92c925b8d 100644
--- a/packages/js-sdk/src/utils.ts
+++ b/packages/js-sdk/src/utils.ts
@@ -106,3 +106,21 @@ export function stripAnsi(text: string): string {
export async function wait(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
+
+/**
+ * Convert data to a Blob, avoiding unnecessary conversions when possible.
+ */
+export function toBlob(
+ data: string | ArrayBuffer | Blob | ReadableStream
+): Blob | Promise<Blob> {
+ // Already a Blob - use directly
+ if (data instanceof Blob) {
+ return data
+ }
+ // String or ArrayBuffer - create Blob
+ if (typeof data === 'string' || data instanceof ArrayBuffer) {
+ return new Blob([data])
+ }
+ // ReadableStream - must consume to get Blob
+ return new Response(data).blob()
+}
diff --git a/packages/js-sdk/tests/sandbox/files/write.test.ts b/packages/js-sdk/tests/sandbox/files/write.test.ts
index d32e099bb..d07de1426 100644
--- a/packages/js-sdk/tests/sandbox/files/write.test.ts
+++ b/packages/js-sdk/tests/sandbox/files/write.test.ts
@@ -228,16 +228,25 @@ sandboxTest('writeFiles with different data types', async ({ sandbox }) => {
const textData = 'Text string data'
const arrayBufferData = new TextEncoder().encode('ArrayBuffer data').buffer
const blobData = new Blob(['Blob data'], { type: 'text/plain' })
+ const streamContent = 'ReadableStream data'
+ const encoder = new TextEncoder()
+ const streamData = new ReadableStream({
+ start(controller) {
+ controller.enqueue(encoder.encode(streamContent))
+ controller.close()
+ },
+ })
const files: WriteEntry[] = [
{ path: 'writefiles_text.txt', data: textData },
{ path: 'writefiles_arraybuffer.txt', data: arrayBufferData },
{ path: 'writefiles_blob.txt', data: blobData },
+ { path: 'writefiles_stream.txt', data: streamData },
]
const infos = await sandbox.files.writeFiles(files)
- assert.equal(infos.length, 3)
+ assert.equal(infos.length, 4)
// Verify text file
const textContent = await sandbox.files.read('writefiles_text.txt')
@@ -253,6 +262,10 @@ sandboxTest('writeFiles with different data types', async ({ sandbox }) => {
const blobContent = await sandbox.files.read('writefiles_blob.txt')
assert.equal(blobContent, 'Blob data')
+ // Verify ReadableStream file
+ const streamFileContent = await sandbox.files.read('writefiles_stream.txt')
+ assert.equal(streamFileContent, streamContent)
+
if (isDebug) {
for (const file of files) {
await sandbox.files.remove(file.path)
diff --git a/packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py b/packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py
index 1fd572a34..d363553ee 100644
--- a/packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py
+++ b/packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py
@@ -1,6 +1,6 @@
import httpcore
import httpx
-from io import IOBase
+from io import IOBase, TextIOBase
from packaging.version import Version
from typing import AsyncIterator, IO, List, Literal, Optional, overload, Union
from e2b.sandbox.filesystem.filesystem import WriteEntry
@@ -209,10 +209,15 @@ class Filesystem:
httpx_files = []
for file in files:
file_path, file_data = file["path"], file["data"]
- if isinstance(file_data, str) or isinstance(file_data, bytes):
+ if isinstance(file_data, (str, bytes)):
+ # str and bytes can be passed directly
httpx_files.append(("file", (file_path, file_data)))
- elif isinstance(file_data, IOBase):
+ elif isinstance(file_data, TextIOBase):
+ # Text streams must be read first
httpx_files.append(("file", (file_path, file_data.read())))
+ elif isinstance(file_data, IOBase):
+ # Binary streams can be passed directly
+ httpx_files.append(("file", (file_path, file_data)))
else:
raise InvalidArgumentException(
f"Unsupported data type for file {file_path}"
diff --git a/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py b/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py
index 960779c70..7f23d1133 100644
--- a/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py
+++ b/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py
@@ -1,4 +1,4 @@
-from io import IOBase
+from io import IOBase, TextIOBase
from typing import IO, Iterator, List, Literal, Optional, overload, Union
from e2b.sandbox.filesystem.filesystem import WriteEntry
@@ -210,10 +210,15 @@ class Filesystem:
httpx_files = []
for file in files:
file_path, file_data = file["path"], file["data"]
- if isinstance(file_data, str) or isinstance(file_data, bytes):
+ if isinstance(file_data, (str, bytes)):
+ # str and bytes can be passed directly
httpx_files.append(("file", (file_path, file_data)))
- elif isinstance(file_data, IOBase):
+ elif isinstance(file_data, TextIOBase):
+ # Text streams must be read first
httpx_files.append(("file", (file_path, file_data.read())))
+ elif isinstance(file_data, IOBase):
+ # Binary streams can be passed directly
+ httpx_files.append(("file", (file_path, file_data)))
else:
raise InvalidArgumentException(
f"Unsupported data type for file {file_path}"
diff --git a/packages/python-sdk/tests/async/sandbox_async/files/test_write.py b/packages/python-sdk/tests/async/sandbox_async/files/test_write.py
index 336d4ed11..13925ed64 100644
--- a/packages/python-sdk/tests/async/sandbox_async/files/test_write.py
+++ b/packages/python-sdk/tests/async/sandbox_async/files/test_write.py
@@ -138,3 +138,39 @@ async def test_write_with_secured_envd(async_sandbox_factory):
read_content = await sbx.files.read(filename)
assert read_content == content
+
+
+async def test_write_files_with_different_data_types(
+ async_sandbox: AsyncSandbox, debug
+):
+ text_data = "Text string data"
+ bytes_data = b"Bytes data"
+ bytes_io_data = io.BytesIO(b"BytesIO data")
+ string_io_data = io.StringIO("StringIO data")
+
+ files = [
+ WriteEntry(path="writefiles_text.txt", data=text_data),
+ WriteEntry(path="writefiles_bytes.bin", data=bytes_data),
+ WriteEntry(path="writefiles_bytesio.bin", data=bytes_io_data),
+ WriteEntry(path="writefiles_stringio.txt", data=string_io_data),
+ ]
+
+ infos = await async_sandbox.files.write_files(files)
+
+ assert len(infos) == 4
+
+ text_content = await async_sandbox.files.read("writefiles_text.txt")
+ assert text_content == text_data
+
+ bytes_content = await async_sandbox.files.read("writefiles_bytes.bin")
+ assert bytes_content == "Bytes data"
+
+ bytes_io_content = await async_sandbox.files.read("writefiles_bytesio.bin")
+ assert bytes_io_content == "BytesIO data"
+
+ string_io_content = await async_sandbox.files.read("writefiles_stringio.txt")
+ assert string_io_content == "StringIO data"
+
+ if debug:
+ for file in files:
+ await async_sandbox.files.remove(file["path"])
diff --git a/packages/python-sdk/tests/sync/sandbox_sync/files/test_write.py b/packages/python-sdk/tests/sync/sandbox_sync/files/test_write.py
index d9dd9b6bf..f5c5a43f7 100644
--- a/packages/python-sdk/tests/sync/sandbox_sync/files/test_write.py
+++ b/packages/python-sdk/tests/sync/sandbox_sync/files/test_write.py
@@ -138,3 +138,37 @@ def test_write_with_secured_envd(sandbox_factory):
read_content = sbx.files.read(filename)
assert read_content == content
+
+
+def test_write_files_with_different_data_types(sandbox, debug):
+ text_data = "Text string data"
+ bytes_data = b"Bytes data"
+ bytes_io_data = io.BytesIO(b"BytesIO data")
+ string_io_data = io.StringIO("StringIO data")
+
+ files = [
+ WriteEntry(path="writefiles_text.txt", data=text_data),
+ WriteEntry(path="writefiles_bytes.bin", data=bytes_data),
+ WriteEntry(path="writefiles_bytesio.bin", data=bytes_io_data),
+ WriteEntry(path="writefiles_stringio.txt", data=string_io_data),
+ ]
+
+ infos = sandbox.files.write_files(files)
+
+ assert len(infos) == 4
+
+ text_content = sandbox.files.read("writefiles_text.txt")
+ assert text_content == text_data
+
+ bytes_content = sandbox.files.read("writefiles_bytes.bin")
+ assert bytes_content == "Bytes data"
+
+ bytes_io_content = sandbox.files.read("writefiles_bytesio.bin")
+ assert bytes_io_content == "BytesIO data"
+
+ string_io_content = sandbox.files.read("writefiles_stringio.txt")
+ assert string_io_content == "StringIO data"
+
+ if debug:
+ for file in files:
+ sandbox.files.remove(file["path"])