All Products
Search
Document Center

Agent Sandbox:Mount an OSS Volume

Last Updated:Oct 19, 2026

When you create a Sandbox, use volume_mounts to mount an existing Object Storage Service (OSS) Volume to a specified directory.

How it works

After the volume is mounted, the Sandbox accesses OSS through file paths, and the data is not deleted when the Sandbox is released.

To append a mount to a running Sandbox, see Append a volume mount.

Prerequisites

  • You have created an OSS Volume for the destination Team and obtained the volume name.

  • You have prepared an Agent Sandbox API key bound to the same Team. A volume and an API key cannot be used across Teams. For more information, see Create an API Key.

  • You have prepared a Resource Access Management (RAM) role. The role has access permissions on the target OSS path, and its trust policy allows the Function Compute service to assume the role. For more information, see Configure a RAM role.

  • You have prepared the Agent Sandbox API URL, the domain, and an available template.

When you use a custom template of the second-generation runtime (MicroVM), set buildConfig.installStorageClients when you create the template. Template Build then automatically installs the ossfs2 client that the mount requires. For more information, see Step 1: Pre-install storage clients when you create a template in this topic.

Procedure

Step 1: Pre-install storage clients when you create a template

This step applies only if you use a custom template of the second-generation runtime (MicroVM). If you already have a template that meets the requirements, skip this step and go to Step 2: Install the SDK.

When you create a custom template with the second-generation runtime (MicroVM), use buildConfig.installStorageClients to automatically install the persistent storage clients. Set generation: 2 in runtimeConfig.sandboxConfig.

For a request body example and the complete limits, see the "Install storage clients" section in Build and manage custom templates with code.

After you enable this parameter, Template Build pre-installs the ossfs2 client that this topic requires, together with its runtime dependencies, in the template image. This parameter does not create storage resources, perform mounts, or configure the network and access credentials. When you create a Sandbox, you must still configure the mount parameters, the network, and the access credentials as described in this topic. Then verify that the mount is available by reading and writing actual data.

Step 2: Install the SDK

This topic uses the E2B Python SDK as an example. volume_mounts requires E2B Python SDK 2.31.0.

python3 -m venv .venv
source .venv/bin/activate
pip install "e2b==2.31.0"

Step 3: Mount and verify

Environment variables

Environment variable

Purpose

Source or value

E2B_API_KEY

The API key used to access Agent Sandbox.

Obtain the key in the Agent Sandbox console. The key must be bound to the Team that owns the volume.

E2B_API_URL

The Agent Sandbox API endpoint.

Set the value to https://api.<REGION>.sandbox.aliyuncs.com. Use the same region as the target volume.

E2B_DOMAIN

The service domain that the SDK uses.

Set the value to <REGION>.sandbox.aliyuncs.com. Use the same region as the API endpoint, and omit the protocol prefix.

E2B_TEMPLATE

The template used to create the Sandbox. The template can be an official template or a custom template.

Specify the ID or alias of an available template that meets the requirements in Prerequisites in this topic. Obtain the value in the Agent Sandbox console.

OSS_VOLUME_NAME

The name of the OSS Volume to mount.

Use the name set in Create an OSS Volume, or query the name in the volume list. Do not use the volume ID.

Mount example

Set the following parameters in your terminal and replace all placeholders with actual values. For the value of each variable, see Environment variables in this topic.

export E2B_API_KEY="<YOUR-E2B-API-KEY>"
export E2B_API_URL="https://api.<REGION>.sandbox.aliyuncs.com"
export E2B_DOMAIN="<REGION>.sandbox.aliyuncs.com"
export E2B_TEMPLATE="<TEMPLATE-ID-OR-ALIAS>"

export OSS_VOLUME_NAME="<YOUR-VOLUME-NAME>"

The mount directory is inside the Sandbox and corresponds to a key in volume_mounts. A subdirectory of /home, /mnt, /tmp, or /data is recommended. This topic uses /mnt/oss.

The following example reuses the mountConfig.role of the volume to mount the OSS Volume to /mnt/oss. Make sure that the volume stores a valid RAM role. To specify a role in metadata, see Configure a RAM role.

The example creates a separate test file with a random identifier in the mount directory, writes data to the file, and reads the file for verification. The example then deletes the file and releases the Sandbox created in this run. Use a mount configuration that provides read and write permissions.

Save the following code as 01_mount_oss_volume.py:

import os
from uuid import uuid4

from e2b import Sandbox


def require_env(name: str) -> str:
    value = os.environ.get(name, "").strip()
    if not value:
        raise RuntimeError(f"Missing environment variable: {name}")
    return value


mount_dir = "/mnt/oss"
marker_path = f"{mount_dir}/hello-oss-{uuid4().hex}.txt"
sandbox = None
marker_written = False

try:
    sandbox = Sandbox.create(
        template=require_env("E2B_TEMPLATE"),
        timeout=300,
        api_key=require_env("E2B_API_KEY"),
        api_url=require_env("E2B_API_URL"),
        domain=require_env("E2B_DOMAIN"),
        volume_mounts={mount_dir: require_env("OSS_VOLUME_NAME")},
    )

    info = sandbox.get_info()
    print(f"volume_mounts={info.volume_mounts}")

    sandbox.files.write(marker_path, "hello oss\n")
    marker_written = True
    content = sandbox.files.read(marker_path)
    if content != "hello oss\n":
        raise RuntimeError(f"File content mismatch: {content!r}")
finally:
    try:
        if sandbox is not None and marker_written:
            sandbox.files.remove(marker_path)
    finally:
        if sandbox is not None:
            sandbox.kill()

print("OSS Volume read/write verification passed")

Run the example:

python 01_mount_oss_volume.py

If the output shows OSS Volume read/write verification passed, the read and write operations, the test file cleanup, and the Sandbox release are complete.

Behavior notes

  • volume_mounts uses the {mount directory: volume name} format. Do not use a volume ID.

  • When a Sandbox is resumed after it is paused, the volume mount is restored.

  • Releasing a Sandbox does not delete the data in OSS. The example deletes the test file. The retention and cleanup of other data must be handled separately.

  • If you mount a read-only volume, the Sandbox can only read the files in the mount directory.

Limits

  • For security reasons, OSS mounts do not support the SSE-KMS method, which uses Key Management Service (KMS) for server-side encryption.

  • A Sandbox supports a maximum of five OSS mount points. The count includes mounts specified when the Sandbox is created, mounts appended later, and mounts that specify the storage configuration directly.

  • The mount directory must be a normalized absolute Unix path. It cannot be /, and it cannot contain leading or trailing spaces, repeated path separators, or the . or .. path segments.

  • The mount directory cannot be a system directory such as /bin, /opt, /var, or /dev, or a subdirectory of such a directory. The mount directory also cannot contain a hidden directory, such as .cache, whose name starts with ..

  • Mount directories must be unique within a Sandbox.

FAQ

The system reports that the volume cannot be found

If the error message is volume not found, check the volume name in the request, the Team that owns the API key, and the region of the API endpoint. When you mount by name, use an existing volume in the same Team. Do not specify a volume ID.

The volume exists but is reported as unavailable

If the error message contains is not available, the volume exists, but its status does not meet the mount requirements. Query the status and the status reason of the volume by using GetVolume, and mount the volume only after you confirm that it is available. If a failure cause exists, address the cause first. Submitting the mount request again does not make the volume available.

Sandbox creation reports a missing RAM role

If the error message contains OSS mount requires Function role, the Sandbox creation did not obtain the RAM role required for the mount.

The example in this topic obtains the role from the mountConfig.role of the volume. If the metadata of the creation request explicitly sets fc.sandbox.auth.role, that value takes precedence. Check the role configuration actually in use, add a valid Alibaba Cloud Resource Name (ARN) for the role, and then submit the creation request again. For more information, see Configure a RAM role.

The mount fails when the Sandbox is created

Based on the error message, check the bucket name, the object prefix, and the endpoint stored in the volume. If the message indicates that access to the storage is denied, check the RAM role that the Sandbox actually uses and the bucket policy. For information about role selection rules, see Configure a RAM role.

If you use a custom template of the second-generation runtime, also confirm that the storage client is installed as described in Step 1: Pre-install storage clients when you create a template in this topic.

The volume is mounted, but cannot be written to

Check whether the volume is configured with readOnly: true. If the configuration allows read and write, check whether the RAM role that the Sandbox actually uses has write permissions, and whether the bucket policy allows the corresponding operations.

To troubleshoot further, retain the complete error message and the request ID.