Uploading and Downloading Sandbox Files

Transfer input files to a sandbox workspace and download the results produced by commands.

Use file operations with a running sandbox. Local paths identify files on the computer running the CLI or SDK. Sandbox paths identify files inside the runtime, such as /workspace/script.py.

For CLI examples, set your connection values:

profile_name="DEFAULT"
region="us-chicago-1"
sandbox_id="SANDBOX_ID"

Uploading a File

Create a local file named script.py:

print('Hello from the sandbox')

Upload it to the sandbox:

oci --profile "$profile_name" --auth security_token --region "$region" \
  generative-ai-sandbox sandbox-file write \
  --sandbox-id "$sandbox_id" \
  --sandbox-file-path /workspace/script.py \
  --file ./script.py

Run the uploaded script:

oci --profile "$profile_name" --auth security_token --region "$region" \
  generative-ai-sandbox sandbox-command run-and-wait \
  --sandbox-id "$sandbox_id" \
  --command "python3 /workspace/script.py" \
  --timeout PT1M

Downloading a File

Read a sandbox file into a local file. Select a local destination that you want to use for the downloaded content:

oci --profile "$profile_name" --auth security_token --region "$region" \
  generative-ai-sandbox sandbox-file read \
  --sandbox-id "$sandbox_id" \
  --sandbox-file-path /workspace/script.py \
  --local-file ./downloaded-script.py

Download any results that you need before the sandbox stops or expires. Use snapshots to preserve a workspace for creating another sandbox.

Using the Python SDK

Reuse the client setup from Using the Python SDK to initialize an authenticated client. Set the ID of a running sandbox, then upload a local file and read it back. The complete quick-start example stops its sandbox, so use this code before stopping it or use another running sandbox:

sandbox_id = "SANDBOX_ID"

with open("script.py", "rb") as input_file:
    client.write_sandbox_file(sandbox_id, "/workspace/script.py", input_file)

response = client.read_sandbox_file(sandbox_id, "/workspace/script.py")
with open("downloaded-script.py", "wb") as output_file:
    output_file.write(response.data.content)