File Operations
Upload input files to a sandbox, generate output files inside the VM, and download results.
Upload a file
- CLI
- Python
- JavaScript
# Upload via volume files
instavm volume files upload <volume_id> ./data.csv --path data.csv
# Or via SCP after creating a VM
instavm create
scp -P <port> ./data.csv user@<host>:/app/data.csv
from instavm import InstaVM
with InstaVM('your_api_key') as vm:
vm.upload_file('./data.csv', remote_path='/app/data.csv')
result = vm.execute("import pandas as pd; df = pd.read_csv('/app/data.csv'); print(df.shape)")
print(result['output'])
import { InstaVM } from 'instavm';
const client = new InstaVM('your_api_key');
await client.upload([{
name: 'data.csv',
content: fs.readFileSync('./data.csv', 'utf-8'),
path: '/app/data.csv',
}]);
const result = await client.execute("import pandas as pd; df = pd.read_csv('/app/data.csv'); print(df.shape)");
console.log(result.output);
await client.dispose();
Download a file
- CLI
- Python
- JavaScript
# Download via SCP
instavm connect <vm_id>
# Inside VM: generate the file, then exit
scp -P <port> user@<host>:/app/output.csv ./output.csv
# Or via volume files (if using volumes)
instavm volume files download <volume_id> output.csv --out ./output.csv
with InstaVM('your_api_key') as vm:
vm.execute("""
import pandas as pd
df = pd.DataFrame({'x': range(100), 'y': [i**2 for i in range(100)]})
df.to_csv('/app/output.csv', index=False)
print("Saved")
""")
vm.download_file('/app/output.csv', local_path='./output.csv')
import { InstaVM } from 'instavm';
const client = new InstaVM('your_api_key');
await client.execute(`
import pandas as pd
df = pd.DataFrame({'x': range(100), 'y': [i**2 for i in range(100)]})
df.to_csv('/app/output.csv', index=False)
print("Saved")
`);
// Download via SDK methods or SCP
await client.dispose();
Generate and download an image
- CLI
- Python
- JavaScript
# Generate in VM, then download via SCP
instavm create
instavm connect <vm_id>
# Inside VM: pip install matplotlib && python3 plot_script.py
scp -P <port> user@<host>:/app/plot.png ./plot.png
import os
with InstaVM('your_api_key') as vm:
vm.execute("""
import subprocess, sys
subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-q', 'matplotlib'])
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
plt.figure(figsize=(10, 6))
plt.plot(x, np.sin(x), label='sin(x)')
plt.plot(x, np.cos(x), label='cos(x)')
plt.legend()
plt.savefig('/app/plot.png', dpi=150, bbox_inches='tight')
print("Plot saved")
""")
vm.download_file('/app/plot.png', local_path='./plot.png')
print(f"Downloaded: {os.path.getsize('./plot.png')} bytes")
import { InstaVM } from 'instavm';
import fs from 'fs';
const client = new InstaVM('your_api_key');
await client.execute(`
import subprocess, sys
subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-q', 'matplotlib'])
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
plt.figure(figsize=(10, 6))
plt.plot(x, np.sin(x), label='sin(x)')
plt.plot(x, np.cos(x), label='cos(x)')
plt.legend()
plt.savefig('/app/plot.png', dpi=150, bbox_inches='tight')
print("Plot saved")
`);
// Download the generated plot
await client.dispose();
Get file content without saving
- Python
- JavaScript
result = vm.download_file('/app/output.txt')
print(result['content'])
const result = await client.execute("cat /app/output.txt", { language: 'bash' });
console.log(result.output);
Process uploaded files
Upload a file, process it in the sandbox, download the result:
- CLI
- Python
- JavaScript
# Upload, process via SSH, then download
instavm create
scp -P <port> ./input.txt user@<host>:/app/input.txt
instavm connect <vm_id>
# Inside VM: python3 process.py
scp -P <port> user@<host>:/app/output.txt ./output.txt
with InstaVM('your_api_key') as vm:
# Upload input
vm.upload_file('./input.txt', remote_path='/app/input.txt')
# Process
vm.execute("""
with open('/app/input.txt') as f:
lines = f.readlines()
# Transform: uppercase and number each line
with open('/app/output.txt', 'w') as f:
for i, line in enumerate(lines, 1):
f.write(f"{i}: {line.strip().upper()}\\n")
print(f"Processed {len(lines)} lines")
""")
# Download output
vm.download_file('/app/output.txt', local_path='./output.txt')
import { InstaVM } from 'instavm';
import fs from 'fs';
const client = new InstaVM('your_api_key');
// Upload input
await client.upload([{
name: 'input.txt',
content: fs.readFileSync('./input.txt', 'utf-8'),
path: '/app/input.txt',
}]);
// Process
await client.execute(`
with open('/app/input.txt') as f:
lines = f.readlines()
with open('/app/output.txt', 'w') as f:
for i, line in enumerate(lines, 1):
f.write(f"{i}: {line.strip().upper()}\\n")
print(f"Processed {len(lines)} lines")
`);
await client.dispose();
File paths in the sandbox
| Path | Description |
|---|---|
/app | Default working directory, use this for your files |
/tmp | Temporary files |
/root | Home directory |
Binary files
The upload/download methods handle binary files (images, PDFs, videos) without any special configuration. The content is transferred as-is.
Next steps
- Python SDK: File Operations -- full method reference
- Code Execution -- running code that produces files
- REST API: Files -- API endpoints