Skip to main content

File Operations

Upload files to a sandbox and download results back to your local machine.

upload_file(file_path, remote_path, recursive=False)

Upload a local file to the VM:

from instavm import InstaVM

with InstaVM('your_api_key') as vm:
result = vm.upload_file(
file_path='/path/to/local/file.txt',
remote_path='/app/file.txt'
)
print(f"Uploaded {result['filename']}, {result['size']} bytes")

# Use the file in code
vm.execute("with open('/app/file.txt') as f: print(f.read())")

download_file(filename, local_path=None)

Download a file from the VM:

with InstaVM('your_api_key') as vm:
# Create a file on the VM
vm.execute("with open('/app/output.txt', 'w') as f: f.write('Hello World')")

# Download to local path
result = vm.download_file(
filename='/app/output.txt',
local_path='/path/to/local/output.txt'
)
print(f"Downloaded {result['filename']}, {result['size']} bytes")

Get content without saving

result = vm.download_file(filename='/app/output.txt')
print(f"Content: {result['content']}")

Handling binary files

For images, PDFs, and other binary outputs:

import os

with InstaVM('your_api_key') as vm:
vm.execute("""
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x))
plt.savefig('/app/plot.png', dpi=150)
print("Plot saved")
""")

vm.download_file(
filename='/app/plot.png',
local_path='plot.png'
)
print(f"Downloaded {os.path.getsize('plot.png')} bytes")

File paths

Files in the sandbox use standard Linux paths. The default working directory is /app.

PathDescription
/appDefault working directory
/tmpWritable temporary directory
/rootHome directory in root-based images

Next steps