> ## Documentation Index
> Fetch the complete documentation index at: https://novita.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Daten in die Sandbox hochladen

export const SandboxConfigHint = () => {
  if (typeof document === "undefined") {
    return null;
  } else {
    return <Note>Before running the example code in this document, please ensure you have properly configured environment variables. For details, please refer to <a href="/docs/de/guides/sandbox-your-first-agent-sandbox#configure-environment-variables">Configure Environment Variables</a>.</Note>;
  }
};

Sie können die Methode `files.write()` verwenden, um Daten in die Sandbox hochzuladen.

<SandboxConfigHint />

## Einzelne Datei hochladen

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import fs from 'fs'
  import { Sandbox } from 'novita-sandbox/code-interpreter'

  const sandbox = await Sandbox.create()

  // Read file from local filesystem
  const localFilePath = '../local-test-file'
  const content = fs.readFileSync(localFilePath, 'utf8')

  // Upload file to sandbox
  const filePathInSandbox = '/tmp/test-file'
  const result = await sandbox.files.write(filePathInSandbox, content)
  console.log(result)

  await sandbox.kill()
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.code_interpreter import Sandbox

  sandbox = Sandbox.create()

  local_file_path = '../local-test-file'
  with open(local_file_path, "rb") as file:
    file_path_in_sandbox = '/tmp/test-file'
    result = sandbox.files.write(file_path_in_sandbox, file)
    print(result)

  sandbox.kill()
  ```
</CodeGroup>

## Mit vorsignierter URL hochladen

Vorsignierte Upload-URLs ermöglichen es Benutzern, Dateien sicher aus Umgebungen hochzuladen, die keine Novita SDK-Anmeldedaten enthalten, z. B. Webbrowser.
Erstellen Sie die Sandbox mit der Option `secure: true`, und verwenden Sie dann das SDK auf Ihrem vertrauenswürdigen Backend, um die Upload-URL zu generieren. Geben Sie sie nur an Benutzer zurück, die von Ihrer Anwendung autorisiert wurden, und legen Sie eine kurze Ablaufzeit fest. Die URL fungiert bis zu ihrem Ablauf als temporäre Bearer-Anmeldeinformation.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox/code-interpreter'

  const sandbox = await Sandbox.create({ secure: true })

  const filePathInSandbox = '/tmp/test-file'

  // Generate pre signature upload URL
  const publicUploadUrl = await sandbox.uploadUrl(filePathInSandbox, {
    useSignatureExpiration: 120, // optional, in seconds
  })

  // Simulate browser behavior
  const form = new FormData()
  form.append('file', new Blob(['file content']), 'test-file')

  const uploadRes = await fetch(publicUploadUrl, {
    method: 'POST',
    body: form,
  })
  if (!uploadRes.ok) {
    throw new Error(`Upload failed: ${uploadRes.status} ${await uploadRes.text()}`)
  }

  // SDK Read Back Verification
  const result = await sandbox.files.read(filePathInSandbox)
  console.log(result)

  await sandbox.kill()
  ```

  ```python Python icon="python" theme={"system"}
  from pathlib import Path

  import requests
  from novita_sandbox.code_interpreter import Sandbox

  sandbox = Sandbox.create(secure=True)

  local_file_path = Path("../local-test-file")
  file_path_in_sandbox = "/tmp/test-file"

  # Generate pre signature upload URL
  signed_url = sandbox.upload_url(
      path=file_path_in_sandbox,
      use_signature_expiration=120  # optional, in seconds
  )

  # Simulate browser behavior
  with open(local_file_path, "rb") as f:
      response = requests.post(
          signed_url,
          files={"file": (local_file_path.name, f)},
          timeout=60,
      )
  response.raise_for_status()

  # SDK Read Back Verification
  result = sandbox.files.read(file_path_in_sandbox)
  print(result)

  sandbox.kill()
  ```
</CodeGroup>

## Verzeichnis / mehrere Dateien hochladen

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import fs from 'fs'
  import path from 'path'

  import { Sandbox } from 'novita-sandbox/code-interpreter'

  const sandbox = await Sandbox.create()

  const readDirectoryFiles = (directoryPath) => {
    const files = fs.readdirSync(directoryPath);

    const filesArray = files
      .filter(file => {
        const fullPath = path.join(directoryPath, file);
        return fs.statSync(fullPath).isFile();
      })
      .map(file => {
        const filePath = path.join(directoryPath, file);
      
        return {
          path: filePath,
          data: fs.readFileSync(filePath, 'utf8')
        };
      });

    return filesArray;
  };

  const localDirectoryPath = '../local-test-dir'
  const files = readDirectoryFiles(localDirectoryPath);
  console.log(files); 

  files.forEach(file => {
    file.path = file.path.replace('../local-test-dir', '/tmp')
  })

  const result = await sandbox.files.write(files)
  console.log(result)

  await sandbox.kill()
  ```

  ```python Python icon="python" theme={"system"}
  import os
  from novita_sandbox.code_interpreter import Sandbox

  sandbox = Sandbox.create()

  def read_directory_files(directory_path):
      files = []

      for filename in os.listdir(directory_path):
          file_path = os.path.join(directory_path, filename)
          
          if os.path.isfile(file_path):
              with open(file_path, "rb") as file:
                  files.append({
                      'path': file_path,
                      'data': file.read()
                  })
      return files

  local_directory_path = '../local-test-dir'
  files = read_directory_files(local_directory_path)
  print(files)

  for file in files:
      file['path'] = file['path'].replace('../local-test-dir', '/tmp')

  result = sandbox.files.write_files(files)
  print(result)

  sandbox.kill()
  ```
</CodeGroup>
