File Operations

Koyeb Sandboxes are currently in public preview.

Sandbox File and Directory Operations

Sandbox environments are designed to be fully flexible, making them suitable for any type of AI code generation or testing. This flexibility includes the capability to take any actions required on files and directories: reading, writing, updating, moving, deleting, and so on.

Sandbox actions can be taken synchronously or asynchronously to suit the needs of your environment. Refer to the sandbox creation documentation to learn how to set up your sandbox to handle synchronous or asynchronous operations.

When all sandbox actions are complete, delete the sandbox environment. Any files or folders that were changed during the sandbox lifecycle will not be available after deletion.

File operations

Read a file

The following shows how to read a file:

fs = sandbox.filesystem
 
file_info = fs.read_file("/tmp/hello.txt")
print(file_info.content)

The following shows how to read a file when using Koyeb sandboxes asynchronously:

fs = sandbox.filesystem
 
file_info = await fs.read_file("/tmp/hello.txt")
print(file_info.content)

Write to a file

The following shows how to write to a file:

fs = sandbox.filesystem
 
content = "Hello, Koyeb Sandbox!\nThis is a test file."
fs.write_file("/tmp/hello.txt", content)

The following shows how to write to a file when using Koyeb sandboxes asynchronously:

fs = sandbox.filesystem
 
content = "Hello, Koyeb Sandbox!\nThis is a test file."
await fs.write_file("/tmp/hello.txt", content)

Upload a file

The following code writes a file in the Python environment, and then uploads it to the sandbox:

import os
import tempfile
 
from koyeb import Sandbox
 
fs = sandbox.filesystem
 
# Upload local file to sandbox
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as f:
    f.write("This is a local file\n")
    f.write("Uploaded to Koyeb Sandbox!")
    local_file = f.name
 
try:
    fs.upload_file(local_file, "/tmp/uploaded_file.txt")
    uploaded_info = fs.read_file("/tmp/uploaded_file.txt")
    print(uploaded_info.content)
finally:
    os.unlink(local_file)

The following shows the upload process running asynchronously:

import os
import tempfile
 
from koyeb import Sandbox
 
fs = sandbox.filesystem
 
# Upload local file to sandbox
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as f:
    f.write("This is a local file\n")
    f.write("Uploaded to Koyeb Sandbox!")
    local_file = f.name
 
try:
    await fs.upload_file(local_file, "/tmp/uploaded_file.txt")
    uploaded_info = await fs.read_file("/tmp/uploaded_file.txt")
    print(uploaded_info.content)
finally:
    os.unlink(local_file)

Download a file

The following code writes a file in a sandbox, and then downloads it:

fs.write_file(
    "/tmp/download_source.txt", "Download test content\nMultiple lines"
)
 
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix="_downloaded.txt") as f:
    download_path = f.name
 
try:
    fs.download_file("/tmp/download_source.txt", download_path)
    with open(download_path, "r") as f:
        print(f.read())
finally:
    os.unlink(download_path)

The following shows the download process running asynchronously:

await fs.write_file(
    "/tmp/download_source.txt", "Download test content\nMultiple lines"
)
 
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix="_downloaded.txt") as f:
    download_path = f.name
 
try:
    await fs.download_file("/tmp/download_source.txt", download_path)
    with open(download_path, "r") as f:
        print(f.read())
finally:
    os.unlink(download_path)

Rename a file

The following shows how to rename a file:

fs.rename_file("/tmp/file1.txt", "/tmp/renamed_file.txt")
print(f"Renamed: {fs.exists('/tmp/renamed_file.txt')}")

The following shows how to rename a file when using Koyeb sandboxes asynchronously:

# Rename file
await fs.rename_file("/tmp/file1.txt", "/tmp/renamed_file.txt")
renamed_exists = await fs.exists("/tmp/renamed_file.txt")
print(f"Renamed: {renamed_exists}")

Move a file

The following shows how to move a file:

fs.move_file("/tmp/file2.txt", "/tmp/test_dir/moved_file.txt")
print(f"Moved: {fs.exists('/tmp/test_dir/moved_file.txt')}")

The following shows how to move a file when using Koyeb sandboxes asynchronously:

# Move file
await fs.move_file("/tmp/file2.txt", "/tmp/test_dir/moved_file.txt")
moved_exists = await fs.exists("/tmp/test_dir/moved_file.txt")
print(f"Moved: {moved_exists}")

Copy a file

To copy a file, first read its contents, and then write it to the new location:

# Copy file (read + write)
original_content = fs.read_file("/tmp/renamed_file.txt")
fs.write_file("/tmp/test_dir/copied_file.txt", original_content.content)
print(f"Copied: {fs.exists('/tmp/test_dir/copied_file.txt')}")

The following shows how to copy a file when using Koyeb sandboxes asynchronously:

# Copy file (read + write)
original_content = await fs.read_file("/tmp/renamed_file.txt")
await fs.write_file("/tmp/test_dir/copied_file.txt", original_content.content)
copied_exists = await fs.exists("/tmp/test_dir/copied_file.txt")
print(f"Copied: {copied_exists}")

Delete a file

The following shows how to delete a file:

# Delete file
fs.rm("/tmp/renamed_file.txt")
print(f"Deleted: {not fs.exists('/tmp/renamed_file.txt')}")

The following shows how to delete a file when using Koyeb sandboxes asynchronously:

# Delete file
await fs.rm("/tmp/renamed_file.txt")
deleted_check = not await fs.exists("/tmp/renamed_file.txt")
print(f"Deleted: {deleted_check}")

Directory operations

Make a directory

The following shows how to create directories:

fs = sandbox.filesystem
 
# Create directory
fs.mkdir("/tmp/my_project")
 
# Create nested directories
fs.mkdir("/tmp/my_project/src/utils", recursive=True)

The following shows how to create directories when using Koyeb sandboxes asynchronously:

fs = sandbox.filesystem
 
# Create directory
await fs.mkdir("/tmp/my_project")
 
# Create nested directories
await fs.mkdir("/tmp/my_project/src/utils", recursive=True)

List directories

The following shows how to list the contents of a directory:

# List directory
contents = fs.list_dir("/tmp/my_project")
print(f"Contents: {contents}")

The following shows how to list the contents of a directory when using Koyeb sandboxes asynchronously:

# List directory
contents = await fs.list_dir("/tmp/my_project")
print(f"Contents: {contents}")

Delete a directory

The following shows how to delete a directory:

# Delete directory
fs.rm("/tmp/test_dir", recursive=True)
print(f"Directory deleted: {not fs.exists('/tmp/test_dir')}")

Check if a path exists

The following shows how to check if a path exists:

# Check if path exists
exists = fs.exists("/tmp/my_project")
is_dir = fs.is_dir("/tmp/my_project")
is_file = fs.is_file("/tmp/my_project/src/main.py")

The following shows how to check if a path exists when using Koyeb sandboxes asynchronously:

# Check if path exists
exists = await fs.exists("/tmp/my_project")
is_dir = await fs.is_dir("/tmp/my_project")
is_file = await fs.is_file("/tmp/my_project/src/main.py")