> ## 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.

# Téléverser des données vers le sandbox

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/fr/guides/sandbox-your-first-agent-sandbox#configure-environment-variables">Configure Environment Variables</a>.</Note>;
  }
};

Vous pouvez utiliser la méthode `files.write()` pour téléverser des données vers le sandbox.

<SandboxConfigHint />

## Téléverser un fichier unique

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

## Téléverser avec une URL pré-signée

Les URL de téléversement pré-signées permettent aux utilisateurs de téléverser des fichiers en toute sécurité depuis des environnements qui ne détiennent pas les identifiants du SDK Novita, comme les navigateurs web.
Créez le sandbox avec l’option `secure: true`, puis utilisez le SDK sur votre backend de confiance pour générer l’URL de téléversement. Ne la renvoyez qu’aux utilisateurs autorisés par votre application, et définissez une durée d’expiration courte. L’URL agit comme un identifiant porteur temporaire jusqu’à son expiration.

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

## Téléverser un répertoire / plusieurs fichiers

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