Skip to main content

File Operations

Upload input files to a sandbox, generate output files inside the VM, and download results.

Upload a file

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'])

Download a file

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')

Generate and download an image

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")

Get file content without saving

result = vm.download_file('/app/output.txt')
print(result['content'])

Process uploaded files

Upload a file, process it in the sandbox, download the result:

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')

File paths in the sandbox

PathDescription
/appDefault working directory, use this for your files
/tmpTemporary files
/rootHome 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