Upload data to sandbox

You can upload data to the sandbox using the files.write() method.

Upload single file

import fs from 'fs'
import { Sandbox } from '@e2b/code-interpreter'

const sandbox = await Sandbox.create()

// Read file from local filesystem
const content = fs.readFileSync('/local/path')
// Upload file to sandbox
await sandbox.files.write('/path/in/sandbox', content)

Upload directory / multiple files

const fs = require('fs');
const path = require('path');

import { Sandbox } from '@e2b/code-interpreter'

const sandbox = await Sandbox.create()

// Read all files in the directory and store their paths and contents in an array
const readDirectoryFiles = (directoryPath) => {
  // Read all files in the local directory
  const files = fs.readdirSync(directoryPath);

  // Map files to objects with path and data
  const filesArray = files
    .filter(file => {
      const fullPath = path.join(directoryPath, file);
      // Skip if it's a directory
      return fs.statSync(fullPath).isFile();
    })
    .map(file => {
      const filePath = path.join(directoryPath, file);
    
      // Read the content of each file
      return {
        path: filePath,
        data: fs.readFileSync(filePath, 'utf8')
      };
    });

  return filesArray;
};

// Usage example
const files = readDirectoryContents('/local/dir');
console.log(files); 
// [
//   { path: '/local/dir/file1.txt', data: 'File 1 contents...' },
//   { path: '/local/dir/file2.txt', data: 'File 2 contents...' },
//   ...
// ]

await sandbox.files.write(files)