前期准备
开始操作前,确保您已经完成了 SDK 初始化。如果您还没有初始化 SDK,请先参考快速入门文档完成。
注意:
如果媒体库启用回收站功能,删除文件时会移入回收站而非永久删除。
符号链接所指向的文件不会因为重命名或移动而丢失指向。
虚拟文件不对应实际的 COS 对象存储,仅保存元数据信息,可用于占位或记录外部资源引用。
文件信息
获取文件信息
InfoFile 用于获取文件的详细信息和下载链接,支持获取历史版本文件信息。ctx := context.Background()resp, httpRes, err := apiClient.FileAPI.InfoFile(ctx, "your-library-id", "your-space-id", "/test.txt").AccessToken("your-access-token").UserId("user-id").Info(1).ContentDisposition("inline").Execute()if err != nil {panic(err)}fmt.Printf("File info: %s, Size: %s\\n", *resp.CosUrl, *resp.Size)
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID,如果媒体库为单租户模式,则该参数固定为连字符(-);如果媒体库为多租户模式,则必须指定该参数 | String | 是 |
filePath | 文件路径 | String | 是 |
accessToken | 访问令牌 | String | 否 |
userId | 用户身份识别 | String | 否 |
info | 获取文件信息标识,固定为 1 | float32 | 是 |
historyId | 历史版本 ID,不传默认为最新版 | String | 否 |
contentDisposition | 内容处置方式,inline-内联预览,attachment-附件下载 | String | 否 |
purpose | 用途,download-下载,preview-预览 | String | 否 |
preCheck | 是否预检,传 1 表示仅校验文件是否可预览/下载;设置后响应不再返回 cosUrl | float32 | 否 |
trafficLimit | 流量限制(字节/秒) | Int64 | 否 |
contentCas | 文件内容的 Cas 标识 | String | 否 |
withContentCas | 是否返回 contentCas,0:不返回,1:返回 | Int32 | 否 |
internalDomain | 是否使用内网域名,0:不使用(默认),1:使用内网域名 | Int32 | 否 |
withShortLink | 是否返回短链接,0:不返回(默认),1:返回 | Int32 | 否 |
period | 下载链接有效期(秒) | Int32 | 否 |
preview | 0 或 1,默认为 0 返回下载链接,设置为 1 时返回在线预览链接 | Int32 | 否 |
withFavoriteStatus | 是否返回收藏状态,0:不返回(默认),1:返回 | Int32 | 否 |
返回值说明:
HTTP 状态码:200
查询成功。
响应示例:
{"cosUrl": "https://example.com/file.txt?sign=xxx","type": "file","creationTime": "2024-01-15T10:30:00Z","modificationTime": "2024-01-15T11:30:00Z","contentType": "text/plain","size": "1024","eTag": "\\"abc123\\"","crc64": "1234567890","fileType": "txt","previewByDoc": false,"previewByCI": false,"previewAsIcon": false,"metaData": {"department": "engineering"},"labels": ["文档"],"category": "document","localCreationTime": "2024-01-15T10:30:00Z","localModificationTime": "2024-01-15T10:30:00Z","versionId": 1,"contentCas": "xxx"}
响应字段说明
字段 | 说明 | 类型 |
cosUrl | 带签名的下载链接,签名有效时长约 2 小时 | String |
type | 文件类型 | String |
creationTime | 文件首次完成上传的时间 | DateTime |
modificationTime | 文件最近一次被覆盖的时间 | DateTime |
contentType | 媒体类型 | String |
size | 文件大小(字符串格式) | String |
eTag | 文件 ETag | String |
crc64 | 文件的 CRC64-ECMA182 校验值(字符串格式) | String |
fileType | 文件类型:excel、powerpoint 等 | String |
previewByDoc | 是否可通过 wps 预览 | Boolean |
previewByCI | 是否可通过万象预览 | Boolean |
previewAsIcon | 是否可用预览图当做 icon | Boolean |
metaData | 元数据 | Map |
labels | 简易文件标签列表 | Array |
category | 文件自定义的分类 | String |
localCreationTime | 文件对应的本地创建时间 | DateTime |
localModificationTime | 文件对应的本地修改时间 | DateTime |
versionId | 文件版本号 | Integer |
contentCas | 文件内容的 Cas 标识 | String |
获取照片/视频封面缩略图
GetCover 用于获取照片或视频的封面缩略图。ctx := context.Background()httpRes, err := apiClient.FileAPI.GetCover(ctx, "your-library-id", "your-space-id", "/photo.jpg").AccessToken("your-access-token").UserId("user-id").Preview(1).Size(256).Scale(12).WidthSize(256).HeightSize(256).FrameNumber(1).Execute()// SDK 使用 http.DefaultClient 且未设置 CheckRedirect,会自动跟随 302,// 因此这里拿到的是最终响应(200),响应体为图片/文档内容if err != nil {panic(err)}fmt.Printf("Cover image: %d\\n", httpRes.StatusCode)
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 文件路径 | String | 是 |
accessToken | 访问令牌 | String | 否 |
userId | 用户身份识别 | String | 否 |
preview | 固定传 1 | Int32 | 是 |
size | 缩略图尺寸 | Int32 | 否 |
scale | 缩放比例 | Int32 | 否 |
widthSize | 宽度尺寸 | Int32 | 否 |
heightSize | 高度尺寸 | Int32 | 否 |
frameNumber | 视频帧号(视频文件使用) | Int32 | 否 |
返回值说明
HTTP 状态码:302
重定向到封面图地址;SDK 默认自动跟随重定向,调用方实际拿到的是最终响应(HTTP 200),响应体为图片内容。
获取文档预览
PreviewFile 用于获取文档的 HTML 预览链接。ctx := context.Background()httpRes, err := apiClient.FileAPI.PreviewFile(ctx, "your-library-id", "your-space-id", "/test.docx").AccessToken("your-access-token").UserId("user-id").Preview(1).HistoryId("1").Type_("html").Execute()// SDK 使用 http.DefaultClient 且未设置 CheckRedirect,会自动跟随 302,// 因此这里拿到的是最终响应(200),响应体为图片/文档内容if err != nil {panic(err)}fmt.Printf("File preview: %d\\n", httpRes.StatusCode)
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 文件路径 | String | 是 |
accessToken | 访问令牌 | String | 否 |
userId | 用户身份识别 | String | 否 |
preview | 固定传 1 | Int32 | 是 |
historyId | 历史版本 ID | String | 否 |
type_ | 预览类型,pic 以 JPG 格式预览文档首页,否则以 HTML 格式预览 | String | 否 |
返回值说明
HTTP 状态码:302
重定向到文档预览地址;SDK 默认自动跟随重定向,调用方实际拿到的是最终响应(HTTP 200),响应体为文档预览内容。
检查文件状态
CheckFileStatus 用于查询文件是否存在。本接口为 HEAD 请求,无响应体,仅根据 HTTP 状态码判断文件是否存在(200 存在,404 不存在)。ctx := context.Background()httpRes, err := apiClient.FileAPI.CheckFileStatus(ctx, "your-library-id", "your-space-id", "/test.txt").AccessToken("your-access-token").UserId("user-id").Execute()if err != nil && httpRes == nil {panic(err)}fmt.Printf("File status checked: %d\\n", httpRes.StatusCode)
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 文件路径 | String | 是 |
accessToken | 访问令牌(对于公有读媒体库或租户空间,可不指定) | String | 否 |
librarySecret | 访问媒体库密钥 | String | 否 |
userId | 用户身份识别 | String | 否 |
historyId | 历史版本 ID,不传默认为最新版 | String | 否 |
返回值说明
本接口为 HEAD 请求,无响应体,仅根据 HTTP 状态码判断文件是否存在(200 存在,404 不存在)。
Execute() 的返回值签名为:(*http.Response, error)。根据 inode 获取文件信息
GetFileInfoByInode 用于通过 inode 获取文件详细信息。ctx := context.Background()resp, httpRes, err := apiClient.FileAPI.GetFileInfoByInode(ctx, "your-library-id", "your-space-id", "file-inode-123").AccessToken("your-access-token").Execute()if err != nil {panic(err)}fmt.Printf("File name: %s, Size: %s\\n", resp.Name, resp.Size)
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
inode | 文件 ID | String | 是 |
accessToken | 访问令牌 | String | 否 |
userId | 用户身份识别 | String | 否 |
返回值说明
HTTP 状态码:200
查询成功。
响应示例
{"path": ["documents", "report.txt"],"name": "report.txt","type": "file","creationTime": "2024-01-15T10:30:00Z","modificationTime": "2024-01-15T11:30:00Z","contentType": "text/plain","size": "1024","crc64": "1234567890","contentCas": "xxx"}
响应字段说明
字段 | 说明 | 类型 |
path | 文件目录路径 | Array[String] |
name | 文件目录名称 | String |
type | 文件目录类型:dir-目录或相簿;file-文件 | String |
creationTime | 文件目录创建时间 | DateTime |
modificationTime | 文件最近一次被覆盖的时间或目录内最近一次增删时间 | DateTime |
contentType | 媒体类型(仅非目录返回) | String |
size | 文件目录大小(仅非目录返回,字符串格式) | String |
crc64 | 文件的 CRC64-ECMA182 校验值(仅非目录返回,字符串格式) | String |
contentCas | 文件内容的 Cas 标识 | String |
文件管理
复制文件
CopyFile 用于复制文件到新位置。ctx := context.Background()copyRequest := client.CopyFileRequest{CopyFrom: "/source/test.txt",}resp, httpRes, err := apiClient.FileAPI.CopyFile(ctx, "your-library-id", "your-space-id", "/dest/test.txt").AccessToken("your-access-token").UserId("user-id").ConflictResolutionStrategy("rename").CopyFileRequest(copyRequest).Execute()if err != nil {panic(err)}fmt.Printf("File copied, path: %v\\n", resp.Path)
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 目标文件路径 | String | 是 |
conflictResolutionStrategy | 文件名冲突时的处理方式:ask 冲突时返回 HTTP 409,rename 冲突时自动重命名(默认),overwrite 覆盖已有文件(冲突目标为目录时返回 HTTP 409) | String | 否 |
accessToken | 访问令牌 | String | 否 |
userId | 用户身份识别 | String | 否 |
copyFileRequest | 请求对象 | CopyFileRequest | 是 |
CopyFileRequest 对象说明
字段 | 类型 | 必填 | 说明 |
CopyFrom | string | 是 | 被复制的源文件路径 |
返回值说明
HTTP 状态码:200
复制成功。
响应示例
{"path": ["dest", "test.txt"],"contentCas": "xxx"}
响应字段说明
字段 | 说明 | 类型 |
path | 字符串数组或 null,表示最终的文件路径;null 表示父级目录已被删除 | Array[String] |
contentCas | 文件内容的 Cas 标识 | String |
重命名或移动文件
MoveFile 用于移动文件到新位置或重命名。ctx := context.Background()moveRequest := client.MoveFileRequest{From: "/old-name.txt",}resp, httpRes, err := apiClient.FileAPI.MoveFile(ctx, "your-library-id", "your-space-id", "/new-name.txt").AccessToken("your-access-token").UserId("user-id").ConflictResolutionStrategy("rename").MoveFileRequest(moveRequest).Execute()if err != nil {panic(err)}fmt.Printf("File moved, path: %v\\n", resp.Path)
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 目标文件路径 | String | 是 |
conflictResolutionStrategy | 文件名冲突时的处理方式:ask 冲突时返回 HTTP 409,rename 冲突时自动重命名(默认),overwrite 覆盖已有文件(冲突目标为目录时返回 HTTP 409) | String | 否 |
accessToken | 访问令牌 | String | 否 |
userId | 用户身份识别 | String | 否 |
moveFileRequest | 请求对象 | MoveFileRequest | 是 |
MoveFileRequest 对象说明
字段 | 类型 | 必填 | 说明 |
From | string | 是 | 被重命名或移动的源文件路径 |
返回值说明
HTTP 状态码:200
移动成功。
响应示例
{"path": ["new-name.txt"],"contentCas": "xxx"}
响应字段说明
字段 | 说明 | 类型 |
path | 字符串数组或 null,表示最终的文件路径;null 表示父级目录已被删除 | Array[String] |
contentCas | 文件内容的 Cas 标识 | String |
删除文件
DeleteFile 用于删除指定文件。当回收站功能开启且 permanent=0 时,文件移入回收站;否则文件被直接永久删除。ctx := context.Background()resp, httpRes, err := apiClient.FileAPI.DeleteFile(ctx, "your-library-id", "your-space-id", "/test.txt").AccessToken("your-access-token").UserId("user-id").Permanent(0).Execute()if err != nil {panic(err)}if resp != nil && resp.RecycledItemId != nil {fmt.Printf("File moved to recycle bin, recycledItemId: %d\\n", *resp.RecycledItemId)} else {fmt.Printf("File deleted: %d\\n", httpRes.StatusCode)}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 文件路径 | String | 是 |
accessToken | 访问令牌 | String | 否 |
userId | 用户身份识别 | String | 否 |
permanent | 是否永久删除,0-删除到回收站,1-永久删除 | Int32 | 否 |
返回值说明
HTTP 状态码:200 / 204
删除成功。当回收站功能开启且
permanent=0 时,返回 200 且包含响应体;否则文件被直接永久删除,返回 204 无响应体。响应示例
{"recycledItemId": 123456}
响应字段说明
字段 | 说明 | 类型 |
recycledItemId | 回收站项目 ID,用于从回收站永久删除或恢复指定项目 | Int64 |
创建符号链接
CreateSymlink 用于创建文件的符号链接。符号链接所指向的文件不会因为重命名或移动而丢失指向。ctx := context.Background()symlinkRequest := client.CreateSymlinkRequest{LinkTo: "/source/test.xlsx",}resp, httpRes, err := apiClient.FileAPI.CreateSymlink(ctx, "your-library-id", "your-space-id", "/link/test.xlsx").AccessToken("your-access-token").UserId("user-id").ConflictResolutionStrategy("rename").CreateSymlinkRequest(symlinkRequest).Execute()if err != nil {panic(err)}fmt.Printf("Symlink created, path: %v\\n", resp.Path)
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 符号链接路径 | String | 是 |
accessToken | 访问令牌 | String | 否 |
userId | 用户身份识别 | String | 否 |
createSymlinkRequest | 请求对象 | CreateSymlinkRequest | 是 |
CreateSymlinkRequest 对象说明
字段 | 类型 | 必填 | 说明 |
LinkTo | string | 是 | 指向的源文件路径 |
返回值说明
HTTP 状态码:200
创建成功。
响应示例
{"path": ["link", "test.xlsx"]}
响应字段说明
字段 | 说明 | 类型 |
path | 字符串数组或 null,表示最终的符号链接路径;null 表示父级目录已被删除 | Array[String] |
文档转码
ConvertFile 用于将文档转换为其他格式(异步任务),返回 202 与 taskId,需通过任务管理接口轮询转码结果。ctx := context.Background()convertRequest := client.ConvertFileRequest{ConvertFrom: "/source/test.docx",}resp, httpRes, err := apiClient.FileAPI.ConvertFile(ctx, "your-library-id", "your-space-id", "/dest/test.pdf").AccessToken("your-access-token").UserId("user-id").Convert(1).ConflictResolutionStrategy("rename").ConvertFileRequest(convertRequest).Execute()if err != nil {panic(err)}if httpRes.StatusCode == 202 && resp != nil && resp.TaskId != nil {fmt.Printf("转码任务已提交,taskId: %d\\n", *resp.TaskId)}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 文件路径 | String | 是 |
accessToken | 访问令牌 | String | 否 |
userId | 用户身份识别 | String | 否 |
convert | 固定传 1 | Int32 | 是 |
convertFileRequest | 请求对象 | ConvertFileRequest | 是 |
ConvertFileRequest 对象说明
字段 | 类型 | 必填 | 说明 |
ConvertFrom | string | 是 | 转码源文件路径 |
返回值说明
HTTP 状态码:202
202:转码任务已提交,返回异步任务信息
响应示例
{"taskId": 12345}
响应字段说明
字段 | 说明 | 类型 |
taskId | 异步任务 ID,可用于查询任务状态(202 响应返回) | Int32 |
获取最近使用文件
ListRecentlyUsedFile 用于查看最近使用的文件列表,支持分页、筛选和路径信息返回。ctx := context.Background()typeFilter := client.StringAsListRecentlyUsedFileRequestType(client.PtrString("doc"))recentRequest := client.ListRecentlyUsedFileRequest{Limit: client.PtrInt32(20),FilterActionBy: client.PtrString("preview"),Type: &typeFilter,WithPath: client.PtrBool(true),}resp, httpRes, err := apiClient.RecentAPI.ListRecentlyUsedFile(ctx, "your-library-id", "your-space-id").AccessToken("your-access-token").ListRecentlyUsedFileRequest(recentRequest).Execute()if err != nil {panic(err)}for _, item := range resp.Contents {if item.Name != nil && item.OperationTime != nil {fmt.Printf("File: %s, Action: %s, Time: %s\\n",*item.Name,*item.ActionType,*item.OperationTime)}}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID,如果媒体库为单租户模式,则该参数固定为连字符(-);如果媒体库为多租户模式,则必须指定该参数 | String | 是 |
accessToken | 访问令牌,对于公有读媒体库或租户空间可不指定 | String | 否 |
listRecentlyUsedFileRequest | 请求对象,包含分页、筛选等参数 | ListRecentlyUsedFileRequest | 是 |
ListRecentlyUsedFileRequest 对象说明
字段 | 类型 | 是否必填 | 说明 |
Marker | string | 否 | 用于顺序列出分页的标识,可选参数,不传默认第一页 |
Limit | int32 | 否 | 用于顺序列出分页时本地列出的项目数限制,可选参数,不传则默认 20 |
FilterActionBy | string | 否 | 筛选操作方式,可选,不传返回全部,preview 只返回预览操作,modify 返回编辑操作 |
Type | ListRecentlyUsedFileRequestType | 否 | 文件类型过滤,可选参数 |
WithPath | bool | 否 | 是否返回文件路径,默认 false |
返回值说明
HTTP 状态码:200
查询成功。
响应示例
{"nextMarker": "nextPageMarker","contents": [{"name": "document.pdf","spaceId": "space123","inode": "file123","size": "1024","actionType": "preview","operationTime": "2024-01-15T10:30:00Z","creationTime": "2024-01-14T08:00:00Z","crc64": "1234567890","path": ["documents", "report.pdf"]}]}
响应字段说明
字段 | 说明 | 类型 |
nextMarker | 用于顺序列出分页的标识,仅当不为最后一页时会返回该字段 | String |
contents | 最近使用文件列表的具体内容 | Array |
Contents 内部字段说明
字段 | 说明 | 类型 |
name | 文件名 | String |
spaceId | 空间 ID | String |
inode | 文件 ID | String |
size | 文件大小,为了避免数字精度问题,这里为字符串格式 | String |
actionType | 加入最近使用列表时的操作类型 | String |
operationTime | ISO 8601 格式的日期与时间字符串,表示加入最近使用文件列表的时间 | String |
creationTime | ISO 8601 格式的日期与时间字符串,表示文件的上传时间 | String |
crc64 | 文件的 CRC64-ECMA182 校验值,为了避免数字精度问题,这里为字符串格式 | String |
path | 字符串数组,表示文件的路径,仅当设置了 withPath 为 true 时返回该字段 | Array |
创建虚拟文件
CreateVirtualFile 用于在指定路径创建一个不包含实际内容的虚拟文件记录,可设置媒体类型、元数据、标签、分类和大小。虚拟文件不对应实际的 COS 对象存储,仅保存元数据信息,可用于占位或记录外部资源引用。ctx := context.Background()virtualFileRequest := client.CreateVirtualFileRequest{ContentType: client.PtrString("application/pdf"),MetaData: &map[string]string{"source": "external", "url": "https://example.com/doc.pdf"},Labels: []string{"外部文档", "参考资料"},Category: client.PtrString("document"),Size: client.PtrString("1024"),}resp, httpRes, err := apiClient.FileAPI.CreateVirtualFile(ctx, "your-library-id", "your-space-id", "/virtual/doc.pdf").VirtualFile(1).AccessToken("your-access-token").UserId("user-id").ConflictResolutionStrategy("rename").CreateVirtualFileRequest(virtualFileRequest).Execute()if err != nil {panic(err)}fmt.Printf("Virtual file created: %v\\n", resp.Path)
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 虚拟文件路径 | String | 是 |
virtualFile | 固定传 1 | Int32 | 是 |
accessToken | 访问令牌,对于公有读媒体库或租户空间可不指定 | String | 否 |
librarySecret | 访问媒体库密钥 | String | 否 |
userId | 用户身份识别 | String | 否 |
conflictResolutionStrategy | 文件名冲突时的处理方式,ask/rename/overwrite,默认 rename | String | 否 |
createVirtualFileRequest | 请求对象,包含虚拟文件的扩展参数 | CreateVirtualFileRequest | 否 |
CreateVirtualFileRequest 对象说明
字段 | 类型 | 必填 | 说明 |
ContentType | string | 否 | 虚拟文件的媒体类型,用于标识虚拟文件所代表的资源类型 |
MetaData | map[string]string | 否 | 自定义元数据键值对,key 为小写字符串 |
Labels | []string | 否 | 文件标签列表 |
Category | string | 否 | 文件自定义分类,最大长度 16 字节 |
Size | string | 否 | 虚拟文件的大小(单位:字节),默认为 "0";该值将用于存储配额计算 |
返回值说明
HTTP 状态码:200
创建成功。
响应示例
{"path": ["documents", "virtual-file.txt"],"type": "virtual"}
响应字段说明
字段 | 说明 | 类型 |
path | 字符串数组或 null,表示最终的虚拟文件路径;null 表示目标路径的某级父级目录已被删除 | Array[String] |
type | 固定为 virtual | String |
查询文件删除原因
查询文件删除原因。用于查询指定文件被删除的原因和时间等信息。
方法签名
func (a *FileAPIService) CheckFileDeletion(ctx context.Context, libraryId string, spaceId string, inode string) FileAPICheckFileDeletionRequest
使用示例
基本使用
ctx := context.Background()resp, httpRes, err := apiClient.FileAPI.CheckFileDeletion(ctx, "your-library-id", "your-space-id", "file-inode-123").AccessToken("your-access-token").LibrarySecret("your-library-secret").Execute()if err != nil {panic(err)}fmt.Printf("File deletion reason: %s, deleted at: %v\\n", resp.GetReason(), resp.GetDeletedAt())
参数说明
参数名 | 类型 | 是否必填 | 说明 |
libraryId | string | 是 | 媒体库 ID |
spaceId | string | 是 | 空间 ID,如果媒体库为单租户模式,则该参数固定为连字符(-);如果媒体库为多租户模式,则必须指定该参数 |
inode | string | 是 | 文件唯一标识 ID |
accessToken | string | 否 | 访问令牌,对于公有读媒体库或租户空间,可不指定该参数,否则必须指定该参数 |
librarySecret | string | 否 | 访问媒体库密钥 |
返回值说明
HTTP 状态码:200,查询成功。
响应字段说明
字段 | 说明 | 类型 |
reason | 文件删除的原因 | String |
deletedAt | 文件删除的时间(ISO 8601 格式) | DateTime |
quotaCleanupRecordRetentionDays | quota 超限删除流水保留天数 | Integer |
文件收藏
文件收藏
CreateFavorite 用于收藏指定空间的文件或目录。收藏和取消收藏操作需要提供路径或 inode,二者二选一;如果同时提供,以 inode 为准。注意:
若您使用收藏管理功能,需要具有相应空间的读写权限,且初始化创建 access_token 时必须要传 userId,否则会没有权限。
// 使用路径收藏文件ctx := context.Background()favoriteRequest := client.CreateFavoriteRequest{Path: client.PtrString("/documents/report.pdf"),}resp, httpRes, err := apiClient.FavoriteAPI.CreateFavorite(ctx, "your-library-id", "your-space-id").AccessToken("your-access-token").CreateFavoriteRequest(favoriteRequest).Execute()if err != nil {panic(err)}fmt.Printf("Favorite created successfully: %d\\n", httpRes.StatusCode)fmt.Printf("Inode: %s\\n", *resp.Inode)
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID,如果媒体库为单租户模式,则该参数固定为连字符(-);如果媒体库为多租户模式,则必须指定该参数 | String | 是 |
accessToken | 访问令牌 | String | 否 |
createFavoriteRequest | 收藏请求对象 | CreateFavoriteRequest | 是 |
CreateFavoriteRequest 对象说明
字段 | 类型 | 必填 | 说明 |
Path | string | 否 | 文件目录路径,与 inode 二选一 |
Inode | string | 否 | 文件目录 ID,与 path 二选一,优先级高于 path |
注意:
path 和 inode 二选一,至少提供一个;如果同时提供,以 inode 为准。
返回值说明
HTTP 状态码:200
收藏成功。
响应示例
{"inode": "46bb40dd044f66340006425bd913af6f"}
响应字段说明
字段 | 说明 | 类型 |
inode | 收藏的文件目录 ID | String |
查看收藏列表
ListFavorite 用于查看指定空间的收藏列表,支持分页和排序。ctx := context.Background()resp, httpRes, err := apiClient.FavoriteAPI.ListFavorite(ctx, "your-library-id", "your-space-id").AccessToken("your-access-token").Page(1).PageSize(10).Execute()if err != nil {panic(err)}if resp.TotalNum != nil {fmt.Printf("Page 1 - Total: %d\\n", *resp.TotalNum)}for _, item := range resp.Contents {if item.Name != nil {fmt.Printf("%s\\n", *item.Name)}}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID,如果媒体库为单租户模式,则该参数固定为连字符(-);如果媒体库为多租户模式,则必须指定该参数 | String | 是 |
accessToken | 访问令牌 | String | 否 |
marker | 用于顺序列出分页的标识 | String | 否 |
limit | 用于顺序列出分页时本地列出的项目数限制,默认为 20 | Int32 | 否 |
page | 分页码,默认第一页,不能与 marker 和 limit 参数同时使用 | Int32 | 否 |
pageSize | 分页大小,默认 20,不能与 marker 和 limit 参数同时使用 | Int32 | 否 |
orderBy | 排序字段,按收藏时间排序为 favoriteTime(默认),目前仅支持按收藏时间排序 | String | 否 |
orderByType | 排序方式,升序为 asc,降序为 desc(默认) | String | 否 |
withPath | 是否返回 path,默认为 false | Boolean | 否 |
返回值说明
HTTP 状态码:200
查询成功。
响应示例
{"totalNum": 15,"nextMarker": "nextPageMarker","contents": [{"spaceId": "space123","type": "file","inode": "46bb40dd044f66340006425bd913af6f","name": "document.pdf","size": "1024","creationTime": "2024-01-15T10:30:00Z","modificationTime": "2024-01-15T11:00:00Z","favoriteTime": "2024-01-15T12:00:00Z","fileType": "pdf","path": ["documents", "report.pdf"],"userId": "user123","eTag": "abc123","contentType": "application/pdf","crc64": "1234567890"}]}
响应字段说明
字段 | 说明 | 类型 |
totalNum | 收藏文件目录的总数,仅当使用 page、pageSize 方式分页时会返回 | Integer |
nextMarker | 用于顺序列出分页的标识,仅当使用 marker、limit 方式分页且当前不为最后一页时会返回 | String |
contents | 收藏的文件目录集合 | Array |
Contents 内部字段说明
字段 | 说明 | 类型 |
spaceId | 空间 ID | String |
type | 文件目录类型,如果文件已被删除,则不返回该字段 | String |
inode | 文件或目录 ID | String |
name | 文件或目录名称,如果文件已被删除,则返回空字符串 | String |
size | 文件的大小,如果为目录则不返回该字段,字符串格式 | String |
creationTime | 文件或目录的创建时间 | String |
modificationTime | 文件最近一次被覆盖的时间 | String |
favoriteTime | 文件或目录的收藏时间 | String |
fileType | 文件类型 | String |
path | 字符串数组,文件目录路径 | Array |
userId | 收藏人 ID | String |
eTag | 目录或文件的 ETag | String |
virusAuditStatus | 查毒状态(0-6) | Integer |
labels | 文件标签数组 | Array |
category | 自定义文件分类,比如 image、video、doc 等 | String |
contentType | 媒体类型(仅非目录或相簿返回) | String |
crc64 | 文件的 CRC64-ECMA182 校验值 | String |
previewByDoc | 是否可通过 wps 预览(仅非目录或相簿返回) | Boolean |
previewByCI | 是否可通过万象预览(仅非目录或相簿返回) | Boolean |
previewAsIcon | 是否可用预览图作为 icon(仅非目录或相簿返回) | Boolean |
removedByQuota | 是否因为配额超限而被删除文件(仅非目录或相簿返回) | Boolean |
metaData | 元数据(仅非目录或相簿返回) | Object |
取消收藏
DeleteFavorite 用于取消收藏指定空间的文件或目录。ctx := context.Background()cancelFlag := int32(1)deleteRequest := client.CreateFavoriteRequest{Path: client.PtrString("/documents/report.pdf"),}httpRes, err := apiClient.FavoriteAPI.DeleteFavorite(ctx, "your-library-id", "your-space-id").Cancel(cancelFlag).AccessToken("your-access-token").DeleteFavoriteRequest(deleteRequest).Execute()if err != nil {panic(err)}fmt.Printf("Favorite deleted successfully: %d\\n", httpRes.StatusCode)
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID,如果媒体库为单租户模式,则该参数固定为连字符(-);如果媒体库为多租户模式,则必须指定该参数 | String | 是 |
accessToken | 访问令牌 | String | 否 |
cancel | 取消收藏标志,固定传 1 | Int32 | 是 |
deleteFavoriteRequest | 取消收藏请求对象 | CreateFavoriteRequest | 是 |
DeleteFavoriteRequest(CreateFavoriteRequest)对象说明
字段 | 类型 | 必填 | 说明 |
Path | string | 否 | 文件目录路径,与 inode 二选一 |
Inode | string | 否 | 文件目录 ID,与 path 二选一,优先级高于 path |
注意:
path 和 inode 二选一,至少提供一个;如果同时提供,以 inode 为准。
返回值说明
HTTP 状态码:204
取消收藏成功,无响应体。
增量同步
获取增量游标
获取增量游标。用于获取当前最新的增量游标,作为增量查询变动日志接口的起始点。
方法签名
func (a *FileAPIService) GetDeltaCursor(ctx context.Context, libraryId string, spaceId string) FileAPIGetDeltaCursorRequest
使用示例
基本使用:
ctx := context.Background()resp, httpRes, err := apiClient.FileAPI.GetDeltaCursor(ctx, "your-library-id", "your-space-id").AccessToken("your-access-token").UserId("user-id").Execute()if err != nil {panic(err)}fmt.Printf("Delta cursor: %s\\n", resp.GetCursor())
参数说明
参数名 | 类型 | 是否必填 | 说明 |
libraryId | string | 是 | 媒体库 ID |
spaceId | string | 是 | 空间 ID,如果媒体库为单租户模式,则该参数固定为连字符(-);如果媒体库为多租户模式,则必须指定该参数 |
accessToken | string | 否 | 访问令牌,对于公有读媒体库或租户空间,可不指定该参数,否则必须指定该参数 |
librarySecret | string | 否 | 访问媒体库密钥 |
userId | string | 否 | 用户身份识别,当访问令牌对应的权限为管理员权限且申请访问令牌时的用户身份识别为空时用来临时指定用户身份 |
返回值说明
HTTP 状态码:200,查询成功。
响应字段说明
字段 | 说明 | 类型 |
cursor | 当前最新的增量游标,可作为增量查询变动日志接口的起始点,调用方应将其视为不透明标记进行保存和传递 | String |
查询增量变动日志
查询增量变动日志。用于根据增量游标拉取文件/目录的变动日志,支持分页拉取。
方法签名
func (a *FileAPIService) QueryDeltaLog(ctx context.Context, libraryId string, spaceId string) FileAPIQueryDeltaLogRequest
使用示例
基本使用:
ctx := context.Background()// 首先获取增量游标cursorResp, _, err := apiClient.FileAPI.GetDeltaCursor(ctx, "your-library-id", "your-space-id").AccessToken("your-access-token").Execute()if err != nil {panic(err)}cursor := cursorResp.GetCursor()// 使用游标查询增量变动日志resp, httpRes, err := apiClient.FileAPI.QueryDeltaLog(ctx, "your-library-id", "your-space-id").Cursor(cursor).Limit(100).AccessToken("your-access-token").UserId("user-id").Execute()if err != nil {panic(err)}fmt.Printf("Has more: %v, contents count: %d\\n", resp.GetHasMore(), len(resp.GetContents()))// 遍历变动日志for _, item := range resp.GetContents() {fmt.Printf("Event: %s, Name: %s, Type: %s\\n", item.GetEventType(), item.GetName(), item.GetType())}// 保存最新的 cursor 供下次使用fmt.Printf("Latest cursor saved: %s\\n", cursor)
参数说明
参数名 | 类型 | 是否必填 | 说明 |
libraryId | string | 是 | 媒体库 ID |
spaceId | string | 是 | 空间 ID,如果媒体库为单租户模式,则该参数固定为连字符(-);如果媒体库为多租户模式,则必须指定该参数 |
cursor | string | 是 | 增量游标,首次调用传获取增量游标接口返回的 cursor,后续传上次返回的 cursor |
limit | int32 | 否 | 用于分页时本次拉取的项目数限制,默认 100,最大 1000 |
accessToken | string | 否 | 访问令牌,对于公有读媒体库或租户空间,可不指定该参数,否则必须指定该参数 |
librarySecret | string | 否 | 访问媒体库密钥 |
userId | string | 否 | 用户身份识别,当访问令牌对应的权限为管理员权限且申请访问令牌时的用户身份识别为空时用来临时指定用户身份 |
返回值说明
HTTP 状态码:200,查询成功。
响应字段说明
字段 | 说明 | 类型 |
cursor | 下一次请求使用的增量游标,调用方应将其视为不透明标记进行保存,用于下次增量拉取 | String |
hasMore | 是否还有更多数据,为 true 时应继续使用返回的 cursor 拉取 | Boolean |
contents | 增量变更日志列表 | Array |
contents 数组元素字段说明
字段 | 说明 | 类型 |
eventType | 事件类型(详见下方事件类型说明) | String |
eventTime | 事件发生时间(精确到毫秒),ISO 8601 格式 | DateTime |
inode | 文件/目录的唯一标识 ID | String |
parentInode | 父目录的唯一标识 ID | String |
name | 文件名或目录名 | String |
type | 节点类型:file-文件,dir-目录,symlink-符号链接,virtual-虚拟文件 | String |
size | 文件大小(字符串格式,仅文件类型返回) | String |
eTag | 文件 ETag(仅文件类型返回) | String |
crc64 | 文件的 CRC64-ECMA182 校验值(字符串格式,仅文件类型返回) | String |
contentType | 媒体类型(仅文件类型返回) | String |
category | 文件自定义分类(仅文件类型返回) | String |
fileType | 文件类型:excel、powerpoint、word、image 等(仅文件类型返回) | String |
creationTime | 文件的创建时间或上传时间 | DateTime |
modificationTime | 文件最近一次被覆盖的时间,或者目录内最近一次增删子目录或文件的时间 | DateTime |
localCreationTime | 文件对应的本地创建时间(仅文件类型返回) | DateTime |
localModificationTime | 文件对应的本地修改时间(仅文件类型返回) | DateTime |
userId | 操作者用户 ID | String |
versionId | 版本号(仅文件类型返回) | Integer |
location | 文件位置:0-普通文件/目录,1-回收站中,2-历史版本文件,3-已标记删除的普通文件 | Integer |
removedByQuota | 是否被配额策略删除标记 | Boolean |
linkTo | 软链接指向的目标文件 inode(仅软链接类型返回) | String |
extraInfo | 额外信息,不同事件类型携带不同的扩展信息 | Map |
eventType 事件类型说明
事件类型 | 说明 |
FILE.CREATE | 文件/目录/软链接/虚拟文件创建(包括上传完成、秒传、目录创建、软链接创建、虚拟文件创建) |
FILE.MODIFY | 文件内容修改(包括文件内容更新、覆盖上传) |
FILE.DELETE | 文件彻底删除(非回收站删除) |
FILE.COPY | 文件/目录复制 |
FILE.MOVE | 文件/目录移动(含重命名) |
FILE.TRASH | 文件/目录放入回收站 |
FILE.RESTORE | 文件/目录从回收站恢复 |
FILE.CREATE_OVERWRITE | 创建文件时覆盖已有文件 |
FILE.MOVE_OVERWRITE | 移动文件时覆盖已有文件 |
FILE.COPY_OVERWRITE | 拷贝文件时覆盖已有文件 |
FILE.RESTORE_OVERWRITE | 从回收站恢复时覆盖已有文件 |
TASK.FILE.DELETE | 系统任务触发的文件彻底删除 |
TASK.FILE.TRASH | 系统任务触发的放入回收站操作 |
TASK.FILE.RESTORE | 系统任务触发的从回收站恢复操作 |
RECYCLE.DELETE | 从回收站彻底删除 |
RECYCLE.MODIFY | 回收站项目信息更新 |
HISTORY.CREATE | 历史版本创建 |
HISTORY.DELETE | 历史版本删除 |
HISTORY.LATEST | 设为最新版本(回滚到指定历史版本) |
HISTORY.MODIFY | 历史版本更新 |
高级功能
解压预览
预览压缩包内容列表。用于在不解压的情况下查看压缩包内的文件和目录结构,支持扁平列表和树形结构两种返回格式。
注意:
此功能需在媒体库(library)级别开启
enableFileUncompress 功能后方可使用,未开启时返回 403(FileUncompressNotEnabled)。方法签名
func (a *FileAPIService) PreviewZipFile(ctx context.Context, libraryId string, spaceId string, filePath string) FileAPIPreviewZipFileRequest
使用示例
基本使用(扁平列表):
ctx := context.Background()resp, httpRes, err := apiClient.FileAPI.PreviewZipFile(ctx, "your-library-id", "your-space-id", "/archive.zip").AccessToken("your-access-token").UserId("user-id").ZipPreview(1).Format("flat").Execute()if err != nil {panic(err)}fmt.Printf("File count: %d, truncated: %v\\n", resp.GetFileNumber(), resp.GetIsTruncated())
树形结构:
ctx := context.Background()resp, httpRes, err := apiClient.FileAPI.PreviewZipFile(ctx, "your-library-id", "your-space-id", "/archive.zip").AccessToken("your-access-token").UserId("user-id").ZipPreview(1).Format("tree").Execute()if err != nil {panic(err)}fmt.Printf("File count: %d\\n", resp.GetFileNumber())
参数说明
参数名 | 类型 | 是否必填 | 说明 |
libraryId | string | 是 | 媒体库 ID |
spaceId | string | 是 | 空间 ID,如果媒体库为单租户模式,则该参数固定为连字符(-);如果媒体库为多租户模式,则必须指定该参数 |
filePath | string | 是 | 压缩文件路径 |
previewZip | int32 | 是 | 固定值为 1,表示预览压缩包内容 |
format | string | 否 | 返回格式,flat 为扁平列表(默认),tree 为树形结构 |
password | string | 否 | 加密压缩包的密码 |
accessToken | string | 是 | 访问令牌 |
userId | string | 否 | 用户身份识别 |
返回值说明
Execute() 的返回值签名为:(*PreviewZipFile200Response, *http.Response, error)PreviewZipFile200Response 字段说明
字段 | 类型 | 说明 |
fileNumber | int32 | 压缩包中文件/文件夹数量 |
isTruncated | bool | 是否被截断,压缩包预览最多支持预览前 1000 个文件 |
contents | PreviewZipFile200ResponseContents | 压缩包内的具体内容 |
format=flat 时 contents 为对象数组,每个元素字段
字段 | 类型 | 说明 |
key | string | 文件或目录在压缩包内的完整路径 |
lastModified | time.Time | 文件最后修改时间(ISO 8601 格式) |
uncompressedSize | int32 | 文件解压后大小(字节) |
format=tree 时 contents 为树形结构对象,字段
字段 | 类型 | 说明 |
level | int32 | 目录层级深度,根节点为 0 |
isDir | bool | 是否是文件夹 |
filename | string | 文件或文件夹名(不含路径前缀) |
prename | string | 上一级目录名 |
prefix | string | 上一级目录前缀分隔符 |
key | string | 文件在压缩包内的完整路径(仅文件节点有值) |
lastModified | time.Time | 最后修改时间(仅文件节点有值) |
uncompressedSize | int32 | 文件解压后大小(字节,仅文件节点有值) |
children | []map[string]interface | 子文件/文件夹信息数组 |
使用限制:
大小限制:tar/gz/rar 需小于 128MB,zip/7zip 无大小限制
文件数限制:最多 1000 个,超出部分截断(isTruncated 为 true)
文件解压
解压压缩文件。支持将压缩包解压到指定目录,支持选择性解压和跨空间解压。该接口为异步操作,返回任务 ID,可通过查询任务接口获取解压进度和结果。
注意:
此功能需在媒体库(library)级别开启
enableFileUncompress 功能后方可使用,未开启时返回 403(FileUncompressNotEnabled)。方法签名
func (a *FileAPIService) UncompressFile(ctx context.Context, libraryId string, spaceId string, filePath string) FileAPIUncompressFileRequest
使用示例
整包解压:
ctx := context.Background()uncompressRequest := client.UncompressFileRequest{TargetPath: "/unzip-output",}resp, httpRes, err := apiClient.FileAPI.UncompressFile(ctx, "your-library-id", "your-space-id", "/archive.zip").AccessToken("your-access-token").UserId("user-id").Uncompress(1).UncompressFileRequest(uncompressRequest).Execute()if err != nil {panic(err)}fmt.Printf("Uncompress task created: taskId=%d\\n", resp.GetTaskId())
参数说明
参数名 | 类型 | 是否必填 | 说明 |
libraryId | string | 是 | 媒体库 ID |
spaceId | string | 是 | 空间 ID,如果媒体库为单租户模式,则该参数固定为连字符(-);如果媒体库为多租户模式,则必须指定该参数 |
filePath | string | 是 | 压缩文件路径 |
uncompress | int32 | 是 | 固定值为 1,表示解压文件 |
accessToken | string | 是 | 访问令牌 |
userId | string | 否 | 用户身份识别 |
uncompressFileRequest | UncompressFileRequest | 是 | 解压请求参数 |
UncompressFileRequest 字段说明
字段 | 类型 | 是否必填 | 说明 |
TargetPath | string | 是 | 解压目标目录路径,不能为空;该目录必须在 SMH 中已存在 |
TargetSpaceId | string | 否 | 解压目标空间 ID;不传则默认解压到源文件所在空间,支持跨空间解压(需 admin 权限) |
SelectedFilePaths | []string | 否 | 指定需要解压的文件/文件夹路径列表(路径来自 PreviewZipFile 接口返回的 key 字段);文件夹路径需以 / 结尾;不传或为空则整包解压;目录路径最多 1 个,文件路径最多 1000 个 |
Password | string | 否 | 加密压缩包的密码 |
返回值说明
Execute() 的返回值签名为:(*UncompressFile202Response, *http.Response, error)HTTP 状态码:202(异步任务已创建)
UncompressFile202Response 字段说明:
字段 | 类型 | 说明 |
taskId | int32 | 异步解压任务 ID,用于后续查询任务状态 |
使用限制:
支持格式:zip、tar、gz、7zip、rar、apk