# Upload data
<subtitle>Upload local files to the sandbox. </subtitle>

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

## Upload a single file

```python
from ucloud_sandbox import Sandbox

sandbox = Sandbox.create()

# Read files from local file system
with open("path/to/local/file", "rb") as file:
  # Upload files to the sandbox
  sandbox.files.write("/path/in/sandbox", file)
```

## Upload using pre-signed URL

Sometimes, you may want to allow users from unauthorized environments (such as browsers) to upload files to the sandbox. For this use case, you can use presigned URLs to let users upload files securely.

You just create the sandbox using the `secure=True` option. An upload URL will then be generated with a signature that only allows authorized users to upload files. You can optionally set an expiration time for a URL so that it is only valid for a limited time.

```python
from ucloud_sandbox import Sandbox
import requests

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

# Create a pre-signed file upload URL valid for 10 seconds
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)

# The file is now available in the sandbox and you can read it
content = sandbox.files.read('/path/in/sandbox')
```

## Upload directory/multiple files

```python
import os
from ucloud_sandbox 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 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)
```
