ztodo-file-client
v0.1.3
Published
this is my library
Downloads
16
Readme
文件服务 SDK 文档
1、安装
本 sdk 使用 ts 开发,支持 node.js/浏览器环境使用,用npm install
来安装 sdk
npm install ztodo-file-client
2、初始化与认证
- 在 node.js 环境中,使用
TDNodeClient
类
//以下API用于node.js环境运行
//初始化
import { TDNodeClient } from "ztodo-file-client";
TDNodeClient.init(
{
api: "https://api.xxxx.com",
issuer: "TODO_DEV",
},
true
);
const client = new TDNodeClient();
//用户token jwt认证,商户用户身份 使用文件接口
const token = "xxxxxxxx";
client.jwt(token);
//管理access_token 平台对接 用于管理商户配置
const access_token = "xxxxxxxx";
client.access_token(access_token);
- 在浏览器环境中,使用
TDWebClient
类
//以下API用于浏览器环境运行
//初始化
import { TDWebClient } from "ztodo-file-client";
TDWebClient.init(
{
api: "https://api.xxxx.com",
issuer: "TODO_DEV",
},
true
);
const client = new TDWebClient();
//用户token jwt认证,商户用户身份 使用文件接口
const token = "xxxxxxxx";
client.jwt(token);
//管理access_token 平台对接 用于管理商户配置
const access_token = "xxxxxxxx";
client.access_token(access_token);
3、文件接口
3.1 文件上传
文件上传接口,小于 4M 的小文件通过服务器中转上传到供应商,大文件直接分片上传到服务供应商。
移动云 EOS、腾讯云 COS 需要在配置跨域访问 CORS
import { FileDto, ResultDataDto } from "ztodo-file-client";
const path = "/test/1000/test.jpg"; //上传到存储的文件路径,资源key
const file: File = document.getElementById("test").files[0]; // HTML 页面选择的文件
//文件上传
client
.Post(path, file, `{'orderid':'1021232'}`, true, (percent: number) => {
//上传进度
console.log(`上传进度:${percent}/100`);
})
.then((result: ResultDataDto<FileDto>) => {
console.log(`upload success: ${result.data.path}`);
})
.catch((reason) => {
console.log(`upload error: ${JSON.stringify(reason)}`);
});
3.2 文件查询
import { FileDto, ResultDataDto } from "ztodo-file-client";
const path = "/test/1000/test.jpg"; //上传到存储的文件路径,资源key
//文件查询,支持批量查询
client
.Get([path], [])
.then((result: ResultDataDto<FileDto>) => {
console.log(`get success: ${result.data.path}`);
})
.catch((reason) => {
console.log(`get error: ${JSON.stringify(reason)}`);
});
3.3 文件删除
import { ResultDto } from "ztodo-file-client";
const path = "/test/1000/test.jpg"; //上传到存储的文件路径,资源key
//文件删除,支持批量
client
.Delete([path], [])
.then((result: ResultDto) => {
console.log(`del success: ${result.data.path}`);
})
.catch((reason) => {
console.log(`del error: ${JSON.stringify(reason)}`);
});
3.4 文件打包下载 zip
import { ResultDataDto } from "ztodo-file-client";
const path1 = "/test/1000/test.jpg"; //上传到存储的文件路径,资源key
const path2 = "/test/1000/test2.jpg"; //上传到存储的文件路径,资源key
//文件zip打包下载,支持多个文件
client
.Zip([path1, path2], [])
.then((result: ResultDataDto<string>) => {
console.log(`zip success: ${result.data}`);
})
.catch((reason) => {
console.log(`zip error: ${JSON.stringify(reason)}`);
});
3.5 文件分享
文件分享接口支持传入多个文件,创建一个分享链接,接口返回分享信息(包括分享链接、提取码、分享描述)复制发送给其他人,其他人可以直接下载分享的文件列表
服务端记录分享链接的下载次数、客户端 ip 等
import { ResultDataDto, ShareDto } from "ztodo-file-client";
const path1 = "/test/1000/test.jpg"; //上传到存储的文件路径,资源key
const path2 = "/test/1000/test2.jpg"; //上传到存储的文件路径,资源key
//文件分享,支持多个文件
client
.Share([path1, path2], [], "我给你分享了文件,赶紧下载吧!")
.then((result: ResultDataDto<ShareDto>) => {
console.log(`share success: ${result.data}`);
})
.catch((reason) => {
console.log(`share error: ${JSON.stringify(reason)}`);
});
4、管理接口
4.1 查询商户配置
商户配置信息目前包括:状态、最大容量、默认存储供应商
import { EnterpriseConfDto, ResultDataDto } from "ztodo-file-client";
const owner_id = "1000"; //业务系统 商户id
client
.GetConf(owner_id)
.then((result: ResultDataDto<EnterpriseConfDto>) => {
console.log(`getconf success: ${JSON.stringify(result)}`);
})
.catch((reason) => {
console.log(`getconf error: ${JSON.stringify(reason)}`);
});
4.2 修改商户配置
商户配置信息目前包括:状态、最大容量、默认存储供应商
import { ResultDto } from "ztodo-file-client";
const conf = {
owner_id: "1000", //业务系统 商户id
provider: "Eos", //默认存储供应商
quota: 10240, //最大容量,单位MB
status: 1,
};
client
.SetConf(owner_id)
.then((result: ResultDto) => {
console.log(`setconf success: ${JSON.stringify(result)}`);
})
.catch((reason) => {
console.log(`setconf error: ${JSON.stringify(reason)}`);
});
4.3 查询商户用量统计
商户用量统计包括:已用容量、最大用量、文件数量、供应商使用统计(已用容量、文件数量)
import { EnterpriseStatDto, ResultDataDto } from "ztodo-file-client";
const owner_id = "1000"; //业务系统 商户id
client
.Stat(owner_id)
.then((result: ResultDataDto<EnterpriseStatDto>) => {
console.log(`Stat success: ${JSON.stringify(result)}`);
})
.catch((reason) => {
console.log(`Stat error: ${JSON.stringify(reason)}`);
});