# Upload data to sandbox (/docs/filesystem/upload)

<!-- agent-signals: reading_time_min: 3 · est_tokens: 1665 · updated: 2026-07-30 -->
Related: [Download data from sandbox](/docs/filesystem/download.md), [Get information about a file or directory](/docs/filesystem/info.md), [Attach custom metadata to files](/docs/filesystem/metadata.md), [Read & write files](/docs/filesystem/read-write.md), [Watch sandbox directory for changes](/docs/filesystem/watch.md)

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

## Upload single file [#upload-single-file]

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```js  
      import fs from 'fs'
      import { Sandbox } from 'e2b'

      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)
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```python  
      from e2b import Sandbox

      sandbox = Sandbox.create()

      # Read file from local filesystem
      with open("path/to/local/file", "rb") as file:
        # Upload file to sandbox
        sandbox.files.write("/path/in/sandbox", file)
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>

## Upload with pre-signed URL [#upload-with-pre-signed-url]

Sometimes, you may want to let users from unauthorized environments, like a browser, upload files to the sandbox.
For this use case, you can use pre-signed URLs to let users upload files securely.

All you need to do is create a sandbox with the `secure: true` option. An upload URL will then be generated with a signature that allows only authorized users to upload files.
You can optionally set an expiration time for the URL so that it will be valid only for a limited time.

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```js  
      import { Sandbox } from 'e2b'

      // Start a secured sandbox (all operations must be authorized by default)
      const sandbox = await Sandbox.create(template, { secure: true })

      // Create a pre-signed URL for file upload with a 10 second expiration
      const publicUploadUrl = await sandbox.uploadUrl(
        'demo.txt', {
          useSignatureExpiration: 10_000, // optional
        },
      )

      // Upload a file with a pre-signed URL (this can be used in any environment, such as a browser)
      const form = new FormData()
      form.append('file', 'file content')

      await fetch(publicUploadUrl, { method: 'POST', body: form })

      // File is now available in the sandbox and you can read it
      const content = sandbox.files.read('/path/in/sandbox')
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```python  
      from e2b import Sandbox
      import requests

      # Start a secured sandbox (all operations must be authorized by default)
      sandbox = Sandbox.create(timeout=12_000, secure=True)

      # Create a pre-signed URL for file upload with a 10 second expiration
      signed_url = sandbox.upload_url(path="demo.txt", user="user", use_signature_expiration=10_000)

      form_data = {"file":"file content"}
      requests.post(signed_url, data=form_data)

      # File is now available in the sandbox and you can read it
      content = sandbox.files.read('/path/in/sandbox')
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>

## Upload directory / multiple files [#upload-directory--multiple-files]

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```js  
      import { Sandbox } from 'e2b'

      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 = readDirectoryFiles('/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)
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```python  
      import os
      from e2b import Sandbox

      sandbox = Sandbox.create()

      def read_directory_files(directory_path):
          files = []
          
          # Iterate through all files in the directory
          for filename in os.listdir(directory_path):
              file_path = os.path.join(directory_path, filename)
              
              # Skip if it's a directory
              if os.path.isfile(file_path):
                  # Read file contents in binary mode
                  with open(file_path, "rb") as file:
                      files.append({
                          'path': file_path,
                          'data': file.read()
                      })
          
          return files

      files = read_directory_files("/local/dir")
      print(files)
      # [
      #  {"path": "/local/dir/file1.txt", "data": "File 1 contents..." },
      #   { "path": "/local/dir/file2.txt", "data": "File 2 contents..." },
      #   ...
      # ]

      sandbox.files.write_files(files)
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>
