全部產品
Search
文件中心

Function Compute:檔案元資訊

更新時間:Jul 22, 2026

檔案元資訊用於查看檔案或目錄的名稱、類型、路徑、大小、許可權、屬主、修改時間和符號連結目標等資訊。目錄列表和重新命名結果也會複用這類結構。

擷取元資訊

Python 樣本:

sandbox.files.write("/tmp/demo/input.txt", "hello\n")

info = sandbox.files.get_info("/tmp/demo/input.txt")
print(info.name)
print(info.type)
print(info.path)
print(info.size)
print(info.permissions)

TypeScript 樣本:

await sandbox.files.write("/tmp/demo/input.txt", "hello\n");

const info = await sandbox.files.getInfo("/tmp/demo/input.txt");
console.log(info.name);
console.log(info.type);
console.log(info.path);
console.log(info.size);
console.log(info.permissions);

其他欄位

除上述欄位外,EntryInfo 還包含屬主、屬組和修改時間等資訊。

Python 樣本:

info = sandbox.files.get_info("/tmp/demo/input.txt")
print(info.owner)
print(info.group)
print(info.modified_time)

TypeScript 樣本:

const info = await sandbox.files.getInfo("/tmp/demo/input.txt");
console.log(info.owner);
console.log(info.group);
console.log(info.modifiedTime);

目錄管理

Python 樣本:

workdir = "/tmp/demo"

sandbox.files.make_dir(workdir)
sandbox.files.write(f"{workdir}/input.txt", "hello\n")

items = sandbox.files.list(workdir)
print(items)

sandbox.files.rename(f"{workdir}/input.txt", f"{workdir}/renamed.txt")
print(sandbox.files.exists(f"{workdir}/renamed.txt"))

sandbox.files.remove(workdir)

TypeScript 樣本:

const workdir = "/tmp/demo";

await sandbox.files.makeDir(workdir);
await sandbox.files.write(`${workdir}/input.txt`, "hello\n");

const items = await sandbox.files.list(workdir);
console.log(items);

await sandbox.files.rename(`${workdir}/input.txt`, `${workdir}/renamed.txt`);
console.log(await sandbox.files.exists(`${workdir}/renamed.txt`));

await sandbox.files.remove(workdir);

檔案狀態

sandbox.files.exists(path) 可用於判斷檔案或目錄是否存在,適合在執行命令前做前置檢查。

Python 樣本:

if not sandbox.files.exists("/tmp/demo/config.json"):
    sandbox.files.write("/tmp/demo/config.json", "{}\n")

TypeScript 樣本:

if (!(await sandbox.files.exists("/tmp/demo/config.json"))) {
  await sandbox.files.write("/tmp/demo/config.json", "{}\n");
}

等待檔案產生

需要等待目錄內檔案變化時,可以使用監聽變化。如果只等待單個檔案出現,也可以使用 sandbox.files.exists() 做有限次數輪詢。

輪詢時應設定逾時和最大重試次數,避免任務長期佔用資源。

自訂中繼資料

雲沙箱當前不支援在寫入檔案時通過 metadata 附加自訂中繼資料。getInfo()、list() 和 rename() 返回的常規檔案元資訊不受影響。詳情參見自訂中繼資料。

使用建議

  • 業務代碼應避免拼接未經校正的使用者輸入路徑,防止越權訪問工作目錄外的檔案。

  • 刪除目錄前,建議先確認路徑首碼,避免誤刪系統目錄或共用工作目錄。

  • 需要長期儲存的檔案應寫入外部儲存,Sandbox 本地檔案只適合當前任務生命週期內使用。