files.read() 方法从 Sandbox 下载数据。
from ppio_sandbox import PPIO
ppio = PPIO()
sandbox = ppio.sandbox.create()
# 在 Sandbox 中创建测试文件
file_path_in_sandbox = '/tmp/test-file'
sandbox.files.write(file_path_in_sandbox, 'test-file-content')
# 从 Sandbox 读取文件
content = sandbox.files.read(file_path_in_sandbox)
# 将文件写入本地文件系统
local_file_path = './local-test-file'
with open(local_file_path, 'w') as file:
file.write(content)
sandbox.kill()
import fs from 'fs'
import { PPIO } from 'ppio-sandbox'
const ppio = new PPIO()
const sandbox = await ppio.sandbox.create()
// 在 Sandbox 中创建测试文件
const filePathInSandbox = '/tmp/test-file'
await sandbox.files.write(filePathInSandbox, 'test-file-content')
// 从 Sandbox 读取文件
const content = await sandbox.files.read(filePathInSandbox)
// 将文件写入本地文件系统
const localFilePath = './local-test-file'
fs.writeFileSync(localFilePath, content)
await sandbox.kill()
使用预签名 URL 下载
预签名下载 URL 允许用户从不持有 PPIO SDK 凭证的环境(如 Web 浏览器)安全地下载文件。 创建 Sandbox 时使用secure: true 选项,然后在你可信的后端使用 SDK 生成下载 URL。仅将其返回给你的应用授权的用户,并设置较短的过期时间。该 URL 在过期前相当于一个临时的 bearer 凭证。
from pathlib import Path
import requests
from ppio_sandbox import PPIO
ppio = PPIO()
sandbox = ppio.sandbox.create(secure=True)
# 在 Sandbox 中创建测试文件(需要 SDK 认证)
file_path_in_sandbox = '/tmp/test-file'
sandbox.files.write(file_path_in_sandbox, 'test-file-content')
# 生成预签名下载 URL(有效期 120 秒,可选)
signed_url = sandbox.download_url(
path=file_path_in_sandbox,
use_signature_expiration=120, # 可选,单位为秒
)
# 模拟"浏览器/未授权环境":不使用 API key,直接 GET 下载
response = requests.get(signed_url, timeout=60)
response.raise_for_status()
content = response.text
# 写入本地文件
local_file_path = Path('./local-test-file')
local_file_path.write_text(content)
print(content)
sandbox.kill()
import fs from 'fs'
import { PPIO } from 'ppio-sandbox'
const ppio = new PPIO()
const sandbox = await ppio.sandbox.create({ secure: true })
// 在 Sandbox 中创建测试文件(需要 SDK 认证)
const filePathInSandbox = '/tmp/test-file'
await sandbox.files.write(filePathInSandbox, 'test-file-content')
// 生成预签名下载 URL(有效期 120 秒,可选)
const publicDownloadUrl = await sandbox.downloadUrl(filePathInSandbox, {
useSignatureExpiration: 120, // 可选,单位为秒
})
// 模拟"浏览器/未授权环境":不使用 API key,直接 GET 下载
const res = await fetch(publicDownloadUrl)
if (!res.ok) {
throw new Error(`Download failed: ${res.status} ${await res.text()}`)
}
const content = await res.text()
// 写入本地文件
const localFilePath = './local-test-file'
fs.writeFileSync(localFilePath, content)
console.log(content)
await sandbox.kill()