# 下载数据
<subtitle>从沙箱下载文件到本地。</subtitle>

您可以使用 `files.read()` 方法从沙箱下载数据。

```python
from ucloud_sandbox import Sandbox

sandbox = Sandbox.create()

# 从沙箱读取文件
content = sandbox.files.read('/path/in/sandbox')
# 将文件写入本地文件系统
with open('/local/path', 'w') as file:
  file.write(content)
```

## 使用预签名 URL 下载

有时，您可能希望让来自未授权环境（如浏览器）的用户从沙箱下载文件。对于这种用例，您可以使用预签名 URL 让用户安全地下载文件。

您只需使用 `secure=True` 选项创建沙箱。然后将生成一个带有签名的下载 URL，该签名仅允许授权用户访问文件。您可以选择性地为 URL 设置过期时间，使其仅在有限时间内有效。

```python
from ucloud_sandbox import Sandbox

# 启动安全沙箱（默认情况下所有操作都必须授权）
sandbox = Sandbox.create(timeout=12_000, secure=True)

# 创建一个有效期为 10 秒的预签名文件下载 URL
# 用户只需访问该 URL 即可下载文件，这在浏览器中也可以使用
signed_url = sbx.download_url(path="demo.txt", user="user", use_signature_expiration=10_000)
```
