跳到主要内容

应用文件存储

容器目录会随版本替换,不适合保存用户上传文件。头像、附件、导出文件等应使用 SDK Storage。

浏览器或 Node.js 上传​

import { xdbPaaS } from "@xinduobang/xdb-paas-sdk/browser";

const object = await xdbPaaS.storage.upload(file, {
fileName: file.name,
contentType: file.type || "application/octet-stream",
});

const {url, expiresAt} = await xdbPaaS.storage.getUrl(object.id);

Node.js 后端也可以上传 Buffer:

import { xdbPaaS } from "@xinduobang/xdb-paas-sdk/server";

const object = await xdbPaaS.storage.upload(Buffer.from("hello"), {
fileName: "hello.txt",
contentType: "text/plain",
});

Python 上传​

from xdb_paas import xdb_paas

storage = xdb_paas.storage.create_client()
saved = storage.upload(
content,
file_name="report.xlsx",
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)
download = storage.get_url(saved["id"])

列出和删除​

JavaScript:

const usage = await xdbPaaS.storage.list();
await xdbPaaS.storage.remove(object.id);

Python:

usage = storage.list()
storage.remove(saved["id"])

正确保存文件引用​

数据库中只保存 Storage Object ID,不保存 getUrl/get_url 返回的 URL。访问 URL 有有效期,需要展示或下载时重新获取。

平台会完成以下限制:

  • 文件按 App 隔离;
  • Preview 与 Production 相互隔离;
  • 上传和下载只使用短期签名地址;
  • 上传完成后核对真实文件大小;
  • 应用无法获得 OSS AccessKey、Bucket 权限或永久公开地址。

当前默认单文件上限为 50 MiB,每个 App 总空间为 20 GiB;Preview 和 Production 文件相互隔离,但共用这个总额度。实际额度以后以开通信息为准。源码上传、Runtime 临时目录和数据库容量提醒见资源与文件限制。