Basic File Operations
Writing Files
- CLI Command
- Python SDK
- JavaScript SDK
Copy
# Write text content to file
gravixlayer sandbox file write <sandbox_id> "/home/user/hello.txt" "Hello World!"
# Write JSON data to file
gravixlayer sandbox file write <sandbox_id> "/home/user/data.json" '{"name": "Alice", "age": 30}'
# Write multiline content
gravixlayer sandbox file write <sandbox_id> "/home/user/config.txt" "line1\nline2\nline3"
Copy
from gravixlayer import GravixLayer
client = GravixLayer()
sandbox = client.sandbox.sandboxes.create(
provider="gravix",
region="eu-west-1",
template="python-base-v1"
)
try:
# Write simple text file
client.sandbox.sandboxes.write_file(
sandbox.sandbox_id,
path="/home/user/hello.txt",
content="Hello World!"
)
print("Text file written successfully")
# Write JSON data
import json
data = {"name": "Alice", "age": 30}
client.sandbox.sandboxes.write_file(
sandbox.sandbox_id,
path="/home/user/data.json",
content=json.dumps(data, indent=2)
)
print("JSON file written successfully")
finally:
client.sandbox.sandboxes.kill(sandbox.sandbox_id)
Copy
import { GravixLayer } from 'gravixlayer';
const client = new GravixLayer();
const sandbox = await client.sandbox.sandboxes.create({
provider: "gravix",
region: "eu-west-1",
template: "python-base-v1"
});
try {
// Write simple text file
await client.sandbox.sandboxes.writeFile(
sandbox.sandbox_id,
{
path: "/home/user/hello.txt",
content: "Hello World!"
}
);
console.log("Text file written successfully");
// Write JSON data
const data = { name: "Alice", age: 30 };
await client.sandbox.sandboxes.writeFile(
sandbox.sandbox_id,
{
path: "/home/user/data.json",
content: JSON.stringify(data, null, 2)
}
);
console.log("JSON file written successfully");
} finally {
await client.sandbox.sandboxes.kill(sandbox.sandbox_id);
}
Copy
Text file written successfully
JSON file written successfully
Reading Files
- CLI Command
- Python SDK
- JavaScript SDK
Copy
# First create a file to read
gravixlayer sandbox file write <sandbox_id> "/home/user/sample.txt" "Hello World!"
# Read the file content
gravixlayer sandbox file read <sandbox_id> "/home/user/sample.txt"
# Read JSON file
gravixlayer sandbox file write <sandbox_id> "/home/user/config.json" '{"debug": true, "port": 8080}'
gravixlayer sandbox file read <sandbox_id> "/home/user/config.json"
Copy
from gravixlayer import GravixLayer
client = GravixLayer()
sandbox = client.sandbox.sandboxes.create(
provider="gravix",
region="eu-west-1",
template="python-base-v1"
)
try:
# Write a file first
client.sandbox.sandboxes.write_file(
sandbox.sandbox_id,
path="/home/user/sample.txt",
content="Hello World!\nThis is line 2."
)
# Read the file content
content = client.sandbox.sandboxes.read_file(
sandbox.sandbox_id,
path="/home/user/sample.txt"
)
print(f"File content:\n{content}")
# Read and parse JSON file
import json
json_data = {"debug": True, "port": 8080}
client.sandbox.sandboxes.write_file(
sandbox.sandbox_id,
path="/home/user/config.json",
content=json.dumps(json_data)
)
json_content = client.sandbox.sandboxes.read_file(
sandbox.sandbox_id,
path="/home/user/config.json"
)
parsed_data = json.loads(json_content)
print(f"JSON data: {parsed_data}")
finally:
client.sandbox.sandboxes.kill(sandbox.sandbox_id)
Copy
import { GravixLayer } from 'gravixlayer';
const client = new GravixLayer();
const sandbox = await client.sandbox.sandboxes.create({
provider: "gravix",
region: "eu-west-1",
template: "python-base-v1"
});
try {
// Write a file first
await client.sandbox.sandboxes.writeFile(
sandbox.sandbox_id,
{
path: "/home/user/sample.txt",
content: "Hello World!\nThis is line 2."
}
);
// Read the file content
const content = await client.sandbox.sandboxes.readFile(
sandbox.sandbox_id,
"/home/user/sample.txt"
);
console.log(`File content:\n${content}`);
// Read and parse JSON file
const jsonData = { debug: true, port: 8080 };
await client.sandbox.sandboxes.writeFile(
sandbox.sandbox_id,
{
path: "/home/user/config.json",
content: JSON.stringify(jsonData)
}
);
const jsonContent = await client.sandbox.sandboxes.readFile(
sandbox.sandbox_id,
"/home/user/config.json"
);
const parsedData = JSON.parse(jsonContent);
console.log(`JSON data:`, parsedData);
} finally {
await client.sandbox.sandboxes.kill(sandbox.sandbox_id);
}
Copy
File content:
FileReadResponse(content='Hello World!\nThis is line 2.', path=None, size=None)
JSON data: {'debug': True, 'port': 8080}
Listing Files
- CLI Command
- Python SDK
- JavaScript SDK
Copy
# Create some files first
gravixlayer sandbox file write <sandbox_id> "/home/user/file1.txt" "Content 1"
gravixlayer sandbox file write <sandbox_id> "/home/user/file2.txt" "Content 2"
gravixlayer sandbox file write <sandbox_id> "/home/user/data.json" '{"key": "value"}'
# List files with details
gravixlayer sandbox run <sandbox_id> ls --args="-la" --args="/home/sandbox"
# List only file names
gravixlayer sandbox run <sandbox_id> ls --args="/home/sandbox"
# List files with specific pattern
gravixlayer sandbox run <sandbox_id> ls --args="/home/sandbox/*.txt"
Copy
from gravixlayer import GravixLayer
client = GravixLayer()
sandbox = client.sandbox.sandboxes.create(
provider="gravix",
region="eu-west-1",
template="python-base-v1"
)
try:
# Create some files
client.sandbox.sandboxes.write_file(
sandbox.sandbox_id,
path="/home/user/file1.txt",
content="Content 1"
)
client.sandbox.sandboxes.write_file(
sandbox.sandbox_id,
path="/home/user/file2.txt",
content="Content 2"
)
client.sandbox.sandboxes.write_file(
sandbox.sandbox_id,
path="/home/user/data.json",
content='{"key": "value"}'
)
# List files with details
result = client.sandbox.sandboxes.run_command(
sandbox.sandbox_id,
command="ls",
args=["-la", "/home/user"]
)
print("Detailed listing:")
print(result.stdout)
# List only file names
result = client.sandbox.sandboxes.run_command(
sandbox.sandbox_id,
command="ls",
args=["/home/user"]
)
print("File names:")
print(result.stdout)
finally:
client.sandbox.sandboxes.kill(sandbox.sandbox_id)
Copy
import { GravixLayer } from 'gravixlayer';
const client = new GravixLayer();
const sandbox = await client.sandbox.sandboxes.create({
provider: "gravix",
region: "eu-west-1",
template: "python-base-v1"
});
try {
// Create some files
await client.sandbox.sandboxes.writeFile(
sandbox.sandbox_id,
{
path: "/home/user/file1.txt",
content: "Content 1"
}
);
await client.sandbox.sandboxes.writeFile(
sandbox.sandbox_id,
{
path: "/home/user/file2.txt",
content: "Content 2"
}
);
await client.sandbox.sandboxes.writeFile(
sandbox.sandbox_id,
{
path: "/home/user/data.json",
content: '{"key": "value"}'
}
);
// List files with details
const detailedResult = await client.sandbox.sandboxes.runCommand(
sandbox.sandbox_id,
{
command: "ls",
args: ["-la", "/home/user"]
}
);
console.log("Detailed listing:");
console.log(detailedResult.stdout);
// List only file names
const simpleResult = await client.sandbox.sandboxes.runCommand(
sandbox.sandbox_id,
{
command: "ls",
args: ["/home/user"]
}
);
console.log("File names:");
console.log(simpleResult.stdout);
} finally {
await client.sandbox.sandboxes.kill(sandbox.sandbox_id);
}
Copy
Detailed listing:
total 12
drwxr-xr-x 2 user user 4096 Jan 27 10:30 .
drwxr-xr-x 3 root root 4096 Jan 27 10:30 ..
-rw-r--r-- 1 user user 15 Jan 27 10:30 data.json
-rw-r--r-- 1 user user 9 Jan 27 10:30 file1.txt
-rw-r--r-- 1 user user 9 Jan 27 10:30 file2.txt
File names:
data.json file1.txt file2.txt
Deleting Files
- CLI Command
- Python SDK
- JavaScript SDK
Copy
# Create files to delete
gravixlayer sandbox file write <sandbox_id> "/home/user/temp.txt" "Temporary content"
gravixlayer sandbox file write <sandbox_id> "/home/user/delete_me.json" '{"temp": true}'
# Delete single file
gravixlayer sandbox run <sandbox_id> rm --args="/home/sandbox/temp.txt"
# Delete multiple files
gravixlayer sandbox run <sandbox_id> rm --args="/home/sandbox/delete_me.json"
# Verify deletion
gravixlayer sandbox run <sandbox_id> ls --args="/home/sandbox"
# Delete with pattern (be careful!)
gravixlayer sandbox file write <sandbox_id> "/home/sandbox/temp1.log" "log1"
gravixlayer sandbox file write <sandbox_id> "/home/sandbox/temp2.log" "log2"
gravixlayer sandbox run <sandbox_id> rm --args="/home/sandbox/*.log"
Copy
from gravixlayer import GravixLayer
client = GravixLayer()
sandbox = client.sandbox.sandboxes.create(
provider="gravix",
region="eu-west-1",
template="python-base-v1"
)
try:
# Create files to delete
client.sandbox.sandboxes.write_file(
sandbox.sandbox_id,
path="/home/user/temp.txt",
content="Temporary content"
)
client.sandbox.sandboxes.write_file(
sandbox.sandbox_id,
path="/home/user/delete_me.json",
content='{"temp": true}'
)
# List files before deletion
result = client.sandbox.sandboxes.run_command(
sandbox.sandbox_id,
command="ls",
args=["/home/user"]
)
print("Files before deletion:")
print(result.stdout)
# Delete files
client.sandbox.sandboxes.run_command(
sandbox.sandbox_id,
command="rm",
args=["/home/user/temp.txt"]
)
client.sandbox.sandboxes.run_command(
sandbox.sandbox_id,
command="rm",
args=["/home/user/delete_me.json"]
)
print("Files deleted successfully")
# Verify deletion
result = client.sandbox.sandboxes.run_command(
sandbox.sandbox_id,
command="ls",
args=["/home/user"]
)
print("Files after deletion:")
print(result.stdout)
finally:
client.sandbox.sandboxes.kill(sandbox.sandbox_id)
Copy
import { GravixLayer } from 'gravixlayer';
const client = new GravixLayer();
const sandbox = await client.sandbox.sandboxes.create({
provider: "gravix",
region: "eu-west-1",
template: "python-base-v1"
});
try {
// Create files to delete
await client.sandbox.sandboxes.writeFile(
sandbox.sandbox_id,
{
path: "/home/user/temp.txt",
content: "Temporary content"
}
);
await client.sandbox.sandboxes.writeFile(
sandbox.sandbox_id,
{
path: "/home/user/delete_me.json",
content: '{"temp": true}'
}
);
// List files before deletion
const beforeResult = await client.sandbox.sandboxes.runCommand(
sandbox.sandbox_id,
{
command: "ls",
args: ["/home/user"]
}
);
console.log("Files before deletion:");
console.log(beforeResult.stdout);
// Delete files
await client.sandbox.sandboxes.runCommand(
sandbox.sandbox_id,
{
command: "rm",
args: ["/home/user/temp.txt"]
}
);
await client.sandbox.sandboxes.runCommand(
sandbox.sandbox_id,
{
command: "rm",
args: ["/home/user/delete_me.json"]
}
);
console.log("Files deleted successfully");
// Verify deletion
const afterResult = await client.sandbox.sandboxes.runCommand(
sandbox.sandbox_id,
{
command: "ls",
args: ["/home/user"]
}
);
console.log("Files after deletion:");
console.log(afterResult.stdout);
} finally {
await client.sandbox.sandboxes.kill(sandbox.sandbox_id);
}
Copy
Files before deletion:
delete_me.json temp.txt
Files deleted successfully
Files after deletion:
(empty or remaining files)
Directory Management
Creating Directories
- CLI Command
- Python SDK
- JavaScript SDK
Copy
# Create single directory
gravixlayer sandbox run <sandbox_id> mkdir --args="/home/sandbox/project"
# Create nested directories
gravixlayer sandbox run <sandbox_id> mkdir --args="-p" --args="/home/sandbox/data/raw/csv"
# Create multiple directories
gravixlayer sandbox run <sandbox_id> mkdir --args="/home/sandbox/logs" --args="/home/sandbox/temp" --args="/home/sandbox/output"
# Verify directory creation
gravixlayer sandbox run <sandbox_id> ls --args="-la" --args="/home/sandbox"
gravixlayer sandbox run <sandbox_id> find --args="/home/sandbox" --args="-type" --args="d"
Copy
from gravixlayer import GravixLayer
client = GravixLayer()
sandbox = client.sandbox.sandboxes.create(
provider="gravix",
region="eu-west-1",
template="python-base-v1"
)
try:
# Create single directory
client.sandbox.sandboxes.run_command(
sandbox.sandbox_id,
command="mkdir",
args=["/home/user/project"]
)
# Create nested directories
client.sandbox.sandboxes.run_command(
sandbox.sandbox_id,
command="mkdir",
args=["-p", "/home/user/data/raw/csv"]
)
# Create multiple directories at once
client.sandbox.sandboxes.run_command(
sandbox.sandbox_id,
command="mkdir",
args=["/home/user/logs", "/home/user/temp", "/home/user/output"]
)
print("Directories created successfully")
# Verify directory creation
result = client.sandbox.sandboxes.run_command(
sandbox.sandbox_id,
command="find",
args=["/home/user", "-type", "d"]
)
print("Created directories:")
print(result.stdout)
finally:
client.sandbox.sandboxes.kill(sandbox.sandbox_id)
Copy
import { GravixLayer } from 'gravixlayer';
const client = new GravixLayer();
const sandbox = await client.sandbox.sandboxes.create({
provider: "gravix",
region: "eu-west-1",
template: "python-base-v1"
});
try {
// Create single directory
await client.sandbox.sandboxes.runCommand(
sandbox.sandbox_id,
{
command: "mkdir",
args: ["/home/user/project"]
}
);
// Create nested directories
await client.sandbox.sandboxes.runCommand(
sandbox.sandbox_id,
{
command: "mkdir",
args: ["-p", "/home/user/data/raw/csv"]
}
);
// Create multiple directories at once
await client.sandbox.sandboxes.runCommand(
sandbox.sandbox_id,
{
command: "mkdir",
args: ["/home/user/logs", "/home/user/temp", "/home/user/output"]
}
);
console.log("Directories created successfully");
// Verify directory creation
const result = await client.sandbox.sandboxes.runCommand(
sandbox.sandbox_id,
{
command: "find",
args: ["/home/user", "-type", "d"]
}
);
console.log("Created directories:");
console.log(result.stdout);
} finally {
await client.sandbox.sandboxes.kill(sandbox.sandbox_id);
}
Copy
Directories created successfully
Created directories:
/home/user
/home/user/data
/home/user/data/raw
/home/user/data/raw/csv
/home/user/logs
/home/user/output
/home/user/project
/home/user/temp
File Upload and Download
Upload Local Files
- CLI Command
- Python SDK
- JavaScript SDK
Copy
# Upload a single file
gravixlayer sandbox file upload <sandbox_id> "./local_file.txt" "/home/user/uploaded.txt"
# Upload with different name
gravixlayer sandbox file upload <sandbox_id> "./data.csv" "/home/user/dataset.csv"
# Upload to specific directory
gravixlayer sandbox file upload <sandbox_id> "./config.json" "/home/user/config/app.json"
# Verify upload
gravixlayer sandbox file read <sandbox_id> "/home/user/uploaded.txt"
Copy
from gravixlayer import GravixLayer
import tempfile
import os
client = GravixLayer()
sandbox = client.sandbox.sandboxes.create(
provider="gravix",
region="eu-west-1",
template="python-base-v1"
)
try:
# Create a temporary local file
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as temp_file:
temp_file.write("This is content from a local file.\nLine 2\nLine 3")
temp_file_path = temp_file.name
try:
# Read local file and upload to sandbox
with open(temp_file_path, 'r') as f:
file_content = f.read()
client.sandbox.sandboxes.write_file(
sandbox.sandbox_id,
path="/home/user/uploaded.txt",
content=file_content
)
print("File uploaded successfully")
# Verify upload by reading the file
uploaded_content = client.sandbox.sandboxes.read_file(
sandbox.sandbox_id,
path="/home/user/uploaded.txt"
)
print(f"Uploaded content:\n{uploaded_content}")
# List files to confirm
result = client.sandbox.sandboxes.run_command(
sandbox.sandbox_id,
command="ls",
args=["-la", "/home/user"]
)
print("Files in sandbox:")
print(result.stdout)
finally:
# Clean up local temp file
os.unlink(temp_file_path)
finally:
client.sandbox.sandboxes.kill(sandbox.sandbox_id)
Copy
import { GravixLayer } from 'gravixlayer';
import fs from 'fs';
import path from 'path';
import os from 'os';
const client = new GravixLayer();
const sandbox = await client.sandbox.sandboxes.create({
provider: "gravix",
region: "eu-west-1",
template: "python-base-v1"
});
try {
// Create a temporary local file
const tempDir = os.tmpdir();
const tempFilePath = path.join(tempDir, 'temp_upload.txt');
const content = "This is content from a local file.\nLine 2\nLine 3";
fs.writeFileSync(tempFilePath, content);
try {
// Read local file and upload to sandbox
const fileContent = fs.readFileSync(tempFilePath, 'utf8');
await client.sandbox.sandboxes.writeFile(
sandbox.sandbox_id,
{
path: "/home/user/uploaded.txt",
content: fileContent
}
);
console.log("File uploaded successfully");
// Verify upload by reading the file
const uploadedContent = await client.sandbox.sandboxes.readFile(
sandbox.sandbox_id,
"/home/user/uploaded.txt"
);
console.log(`Uploaded content:\n${uploadedContent}`);
// List files to confirm
const result = await client.sandbox.sandboxes.runCommand(
sandbox.sandbox_id,
{
command: "ls",
args: ["-la", "/home/user"]
}
);
console.log("Files in sandbox:");
console.log(result.stdout);
} finally {
// Clean up local temp file
fs.unlinkSync(tempFilePath);
}
} finally {
await client.sandbox.sandboxes.kill(sandbox.sandbox_id);
}
Copy
File uploaded successfully
Uploaded content:
This is content from a local file.
Line 2
Line 3
Files in sandbox:
total 4
drwxr-xr-x 2 user user 4096 Jan 27 10:30 .
drwxr-xr-x 3 root root 4096 Jan 27 10:30 ..
-rw-r--r-- 1 user user 45 Jan 27 10:30 uploaded.txt
Download Files from Sandbox
- CLI Command
- Python SDK
- JavaScript SDK
Copy
# Create a file in sandbox first
gravixlayer sandbox file write <sandbox_id> "/home/user/download_me.txt" "This file will be downloaded.\nWith multiple lines."
# Download file from sandbox to local system
gravixlayer sandbox file download <sandbox_id> "/home/user/download_me.txt" "./downloaded_file.txt"
# Download generated file (e.g., from data processing)
gravixlayer sandbox file write <sandbox_id> "/home/user/result.csv" "name,age,city\nAlice,25,NYC\nBob,30,LA"
gravixlayer sandbox file download <sandbox_id> "/home/user/result.csv" "./output.csv"
# Download to different local path
gravixlayer sandbox file download <sandbox_id> "/home/user/result.csv" "./data/processed_data.csv"
# Verify local file
cat ./downloaded_file.txt
Copy
from gravixlayer import GravixLayer
client = GravixLayer()
sandbox = client.sandbox.sandboxes.create(
provider="gravix",
region="eu-west-1",
template="python-base-v1"
)
try:
# Create a file in the sandbox
client.sandbox.sandboxes.write_file(
sandbox.sandbox_id,
path="/home/user/download_me.txt",
content="This file will be downloaded.\nWith multiple lines.\nGenerated in sandbox."
)
# Create a CSV file with data
csv_content = "name,age,city\nAlice,25,NYC\nBob,30,LA\nCharlie,35,Chicago"
client.sandbox.sandboxes.write_file(
sandbox.sandbox_id,
path="/home/user/data.csv",
content=csv_content
)
# Download text file
text_content = client.sandbox.sandboxes.read_file(
sandbox.sandbox_id,
path="/home/user/download_me.txt"
)
with open("downloaded_file.txt", "w") as f:
f.write(text_content)
print(f"Downloaded text file ({len(text_content)} characters)")
# Download CSV file
csv_data = client.sandbox.sandboxes.read_file(
sandbox.sandbox_id,
path="/home/user/data.csv"
)
with open("output.csv", "w") as f:
f.write(csv_data)
print(f"Downloaded CSV file ({len(csv_data)} characters)")
# Verify by reading local files
with open("downloaded_file.txt", "r") as f:
local_content = f.read()
print(f"Local text file content:\n{local_content}")
with open("output.csv", "r") as f:
local_csv = f.read()
print(f"Local CSV content:\n{local_csv}")
finally:
client.sandbox.sandboxes.kill(sandbox.sandbox_id)
Copy
import { GravixLayer } from 'gravixlayer';
import fs from 'fs';
const client = new GravixLayer();
const sandbox = await client.sandbox.sandboxes.create({
provider: "gravix",
region: "eu-west-1",
template: "python-base-v1"
});
try {
// Create a file in the sandbox
await client.sandbox.sandboxes.writeFile(
sandbox.sandbox_id,
{
path: "/home/user/download_me.txt",
content: "This file will be downloaded.\nWith multiple lines.\nGenerated in sandbox."
}
);
// Create a CSV file with data
const csvContent = "name,age,city\nAlice,25,NYC\nBob,30,LA\nCharlie,35,Chicago";
await client.sandbox.sandboxes.writeFile(
sandbox.sandbox_id,
{
path: "/home/user/data.csv",
content: csvContent
}
);
// Download text file
const textContent = await client.sandbox.sandboxes.readFile(
sandbox.sandbox_id,
"/home/user/download_me.txt"
);
fs.writeFileSync("downloaded_file.txt", textContent);
console.log(`Downloaded text file (${textContent.length} characters)`);
// Download CSV file
const csvData = await client.sandbox.sandboxes.readFile(
sandbox.sandbox_id,
"/home/user/data.csv"
);
fs.writeFileSync("output.csv", csvData);
console.log(`Downloaded CSV file (${csvData.length} characters)`);
// Verify by reading local files
const localContent = fs.readFileSync("downloaded_file.txt", "utf8");
console.log(`Local text file content:\n${localContent}`);
const localCsv = fs.readFileSync("output.csv", "utf8");
console.log(`Local CSV content:\n${localCsv}`);
} finally {
await client.sandbox.sandboxes.kill(sandbox.sandbox_id);
}
Copy
Downloaded text file (58 characters)
Downloaded CSV file (55 characters)
Local text file content:
This file will be downloaded.
With multiple lines.
Generated in sandbox.
Local CSV content:
name,age,city
Alice,25,NYC
Bob,30,LA
Charlie,35,Chicago

