前期准备
开始操作前,确保您已经完成了 SDK 初始化。如果您还没有初始化 SDK,请先参考快速入门文档完成。
注意事项:
分享相关接口分为两类:分享管理接口(创建、查询、更新、删除分享等,需要访问令牌)和分享访问接口(通过分享码/提取码访问分享内容,部分无需认证)。
访问分享文件前,需要先通过「验证提取码」接口获取分享访问令牌(有效期 10 分钟),并将其作为 accessToken 传入后续分享访问接口。
仅当 adminEnabled 与 ownerEnabled 同时为 true 时,分享外链才可用。
创建分享
功能说明
CreateShare 用于创建文件或目录的分享链接,支持设置有效期、提取码、预览/下载/转存权限等。使用示例
ctx := context.Background()expireTime := time.Date(2026, 12, 31, 23, 59, 59, 0, time.Local)createReq := client.CreateShareRequest{Name: "report.pdf",FilePath: []string{"/documents/report.pdf"},Config: &client.CreateShareRequestConfig{IsPermanent: client.PtrBool(false),ExpireTime: &expireTime,CanPreview: client.PtrBool(true),CanDownload: client.PtrBool(true),},}resp, httpRes, err := apiClient.ShareAPI.CreateShare(ctx, "your-library-id", "your-space-id").CreateShareRequest(createReq).AccessToken("your-access-token").Execute()if err != nil {panic(err)}if httpRes.StatusCode == 200 {fmt.Printf("分享 ID: %s\\n", *resp.Id)fmt.Printf("分享码: %s\\n", *resp.Code)fmt.Printf("访问地址: %s\\n", *resp.Endpoint)}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | string | 是 |
spaceId | 空间 ID,如果媒体库为单租户模式,则该参数固定为连字符(-);如果媒体库为多租户模式,则必须指定该参数 | string | 是 |
createShareRequest | 创建分享请求对象 | CreateShareRequest | 是 |
accessToken | 访问令牌 | string | 否 |
CreateShareRequest 对象说明
字段 | 参数描述 | 类型 | 是否必填 |
Name | 分享名称 | string | 是 |
FilePath | 分享的文件或目录路径数组,最多 1000 个 | []string | 是 |
Config | 分享配置 | *CreateShareRequestConfig | 否 |
Config 对象说明:IsPermanent(*bool,是否永久有效,默认 false)、ExpireTime(*time.Time,isPermanent 为 false 时必填)、ExtractionCode(*string,提取码,不超过 6 位)、CanPreview/CanDownload/CanSaveToNetdisk/ForbidAnonymousUser(*bool,均默认 false)、PreviewLimit/DownloadLimit(*int32,次数限制,仅单文件分享生效)、UserLimit(*int32,访问人数限制)、ShareToUsers([]string,指定可访问用户,设置后禁止匿名访问)。
返回值说明
HTTP 状态码:200,创建成功。返回 id(分享 ID)、code(分享码)、endpoint(访问地址)、createTime、expireTime、isPermanent、domain(含 shareDomain 数组)。
列出分享
功能说明
ListShares 用于列出当前媒体库下的分享列表,支持分页。使用示例
ctx := context.Background()resp, httpRes, err := apiClient.ShareAPI.ListShares(ctx, "your-library-id").Limit(20).OrderByType("desc").WithFileInfo(1).AccessToken("your-access-token").Execute()if err != nil {panic(err)}if httpRes.StatusCode == 200 {for _, share := range resp.Contents {fmt.Printf("%s (code: %s)\\n", *share.Name, *share.Code)}if resp.HasMore != nil && *resp.HasMore {fmt.Printf("还有更多分享,marker: %s\\n", *resp.Marker)}}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | string | 是 |
limit | 每页数量,默认 10,最大 100 | int32 | 否 |
marker | 分页标识 | string | 否 |
orderBy | 排序字段,默认 createTime | string | 否 |
orderByType | 排序方式:asc(默认)、desc | string | 否 |
creatorId | 按创建者筛选,仅管理员可用 | string | 否 |
withFileInfo | 是否返回文件信息,0(默认)或 1 | int32 | 否 |
accessToken | 访问令牌 | string | 否 |
返回值说明
HTTP 状态码:200,返回 contents 分享数组、marker、hasMore。分享对象主要字段:id、name、code、creatorId、createTime、expireTime、isPermanent、adminEnabled、ownerEnabled、canPreview、canDownload、canSaveToNetdisk、previewLimit/previewUsed、downloadLimit/downloadUsed、status(0 未审核、1 审核中、2 审核通过、3 审核不通过)、userLimit/userLimitUsed、fileInfo(withFileInfo=1 时返回)。
搜索分享
功能说明
SearchShares 用于按名称、创建者、时间范围等条件搜索分享。仅管理员可用,QPS 上限 10。使用示例
ctx := context.Background()searchReq := client.SearchSharesRequest{Name: client.PtrString("report"),OrderBy: client.PtrString("createTime"),OrderByType: client.PtrString("desc"),}resp, httpRes, err := apiClient.ShareAPI.SearchShares(ctx, "your-library-id").SearchSharesRequest(searchReq).Limit(20).AccessToken("your-access-token").Execute()if err != nil {panic(err)}if httpRes.StatusCode == 200 {for _, share := range resp.Contents {fmt.Printf("%s\\n", *share.Name)}}
SearchSharesRequest 对象说明:Name(名称模糊匹配)、CreatorId(创建者精确匹配)、OrderBy(createTime/expireTime/name/creatorId)、OrderByType(asc/desc)、ExpireTimeStart/ExpireTimeEnd、CreateTimeStart/CreateTimeEnd(*time.Time)。query 参数 limit 默认 10 最大 50,marker 分页。
获取分享详情
功能说明
GetShareDetail 用于获取指定分享的详细信息。使用示例
ctx := context.Background()resp, httpRes, err := apiClient.ShareAPI.GetShareDetail(ctx, "your-library-id", "12345").Detail(1).WithFileInfo(1).AccessToken("your-access-token").Execute()if err != nil {panic(err)}if httpRes.StatusCode == 200 {fmt.Printf("名称: %s, 分享码: %s, 是否过期: %v\\n", *resp.Name, *resp.Code, *resp.IsExpired)}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | string | 是 |
shareId | 分享 ID | string | 是 |
detail | 固定值 1,表示获取分享详情 | int32 | 是 |
withFileInfo | 是否返回文件信息,0(默认)或 1 | int32 | 否 |
accessToken | 访问令牌 | string | 否 |
返回值说明
HTTP 状态码:200。主要字段:id、libraryId、name、code、creatorId、creationTime、expireTime、isPermanent、isExpired、extractionCode、ownerEnabled、adminEnabled、status(0-3)、canPreview、canDownload、canSaveToNetdisk、forbidAnonymousUser、previewLimit/previewUsed、downloadLimit/downloadUsed、fileInfo、toUsers、userLimit/userLimitUsed、watermarkText。
获取分享 URL 详情
功能说明
GetShareUrlDetail 用于通过分享码(shareToken)获取分享的基本信息,无需认证,适用于访问者打开分享链接时展示分享概况。使用示例
ctx := context.Background()resp, httpRes, err := apiClient.ShareAPI.GetShareUrlDetail(ctx, "share-token-from-url").Execute()if err != nil {panic(err)}if httpRes.StatusCode == 200 {fmt.Printf("名称: %s, 需要提取码: %v, 是否可用: %v\\n",*resp.Name, *resp.NeedExtractionCode, *resp.Enabled)}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
shareToken | 分享链接中的分享码 | string | 是 |
返回值说明
HTTP 状态码:200。主要字段:name、enabled、isExpired、needExtractionCode、allowAnonymousUser、canPreview、canDownload、canSaveToNetDisc、status、enableShareWatermark、shareWatermarkType、watermarkText、fileName、domain。
更新分享
功能说明
UpdateShare 用于更新分享的名称和配置。不允许修改 shareToUsers;更新后预览/下载次数统计(previewUsed/downloadUsed)会重置为 0。使用示例
ctx := context.Background()expireTime := time.Date(2027, 6, 30, 23, 59, 59, 0, time.Local)updateReq := client.UpdateShareRequest{Name: client.PtrString("report-v2.pdf"),Config: client.UpdateShareRequestConfig{IsPermanent: client.PtrBool(false),ExpireTime: &expireTime,CanPreview: client.PtrBool(true),CanDownload: client.PtrBool(true),},}resp, httpRes, err := apiClient.ShareAPI.UpdateShare(ctx, "your-library-id", "12345").Update(1).UpdateShareRequest(updateReq).AccessToken("your-access-token").Execute()if err != nil {panic(err)}if httpRes.StatusCode == 200 {fmt.Printf("更新成功,访问地址: %s\\n", *resp.Endpoint)}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | string | 是 |
shareId | 分享 ID | string | 是 |
update | 固定值 1,表示更新分享 | int32 | 是 |
updateShareRequest | 更新分享请求对象,Config 字段必填(字段同创建配置,但不支持 shareToUsers) | UpdateShareRequest | 是 |
accessToken | 访问令牌 | string | 否 |
禁用或启用分享
功能说明
SetShareEnabled 用于禁用或启用分享。仅当 adminEnabled 与 ownerEnabled 同时为 true 时,分享外链才可用。使用示例
ctx := context.Background()// 创建者禁用分享httpRes, err := apiClient.ShareAPI.SetShareEnabled(ctx, "your-library-id", "12345").SetEnabled(1).SetShareEnabledRequest(client.SetShareEnabledRequest{OwnerEnabled: client.PtrBool(false),}).AccessToken("your-access-token").Execute()if err != nil {panic(err)}fmt.Printf("Status: %d\\n", httpRes.StatusCode)
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | string | 是 |
shareId | 分享 ID | string | 是 |
setEnabled | 固定值 1,表示禁用或启用分享 | int32 | 是 |
setShareEnabledRequest | 启用状态对象:AdminEnabled(仅管理员可设置)、OwnerEnabled(创建者可设置) | SetShareEnabledRequest | 是 |
accessToken | 访问令牌 | string | 否 |
删除分享
功能说明
DeleteShare 用于删除指定分享,删除后分享链接立即失效。使用示例
ctx := context.Background()httpRes, err := apiClient.ShareAPI.DeleteShare(ctx, "your-library-id", "12345").AccessToken("your-access-token").Execute()if err != nil {panic(err)}if httpRes.StatusCode == 204 {fmt.Println("分享已删除")}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | string | 是 |
shareId | 分享 ID | string | 是 |
accessToken | 访问令牌 | string | 否 |
返回值说明
HTTP 状态码:204,删除成功,无响应体。
验证提取码
功能说明
VerifyExtractionCode 用于校验分享提取码,验证成功后返回分享访问令牌(有效期 10 分钟),后续访问分享文件需携带该令牌。本接口无需认证。使用示例
ctx := context.Background()verifyReq := client.VerifyExtractionCodeRequest{ExtractionCode: "ab12",}resp, httpRes, err := apiClient.ShareAPI.VerifyExtractionCode(ctx, "share-code").VerifyExtractionCodeRequest(verifyReq).Execute()if err != nil {panic(err)}if httpRes.StatusCode == 200 {shareAccessToken := *resp.AccessTokenfmt.Printf("分享访问令牌: %s, 过期时间: %v\\n", shareAccessToken, *resp.ExpireTime)// 后续访问分享文件时,将该令牌作为 accessToken 传入}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
shareCode | 分享码 | string | 是 |
verifyExtractionCodeRequest | 验证请求对象 | VerifyExtractionCodeRequest | 是 |
VerifyExtractionCodeRequest 对象说明
字段 | 参数描述 | 类型 | 是否必填 |
ExtractionCode | 提取码 | string | 是 |
LibraryId | 媒体库 ID,分享要求登录时必填 | *string | 否 |
AccessToken | 用户访问令牌(注意:该字段在请求体中的 JSON 名为 snake_case 的 access_token),分享要求登录时必填 | *string | 否 |
DeviceId | 设备 ID(JSON 名为 device_id) | *string | 否 |
返回值说明
HTTP 状态码:200,验证成功,返回 accessToken(分享访问令牌,有效期 10 分钟)与 expireTime。
列出分享文件
功能说明
ListShareFiles 用于列出分享中的文件和目录,支持分页浏览目录内容。使用示例
ctx := context.Background()resp, httpRes, err := apiClient.ShareAPI.ListShareFiles(ctx, "share-code", "root-inode").List(1).AccessToken(shareAccessToken). // 验证提取码获取的分享访问令牌Limit(50).Execute()if err != nil {panic(err)}if httpRes.StatusCode == 200 {for _, file := range resp.Contents {fmt.Printf("%s (%s)\\n", *file.Name, *file.Type)}}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
shareCode | 分享码 | string | 是 |
inodes | 目录 inode 链(/ 分隔),指定列出的目录层级,最长 64 层 | string | 是 |
list | 固定值 1,表示列出分享文件 | int32 | 是 |
limit | 每页数量,默认 10,取值 0-100 | int32 | 否 |
marker | 分页标识 | string | 否 |
orderBy | 排序字段:name、size、updatedAt | string | 否 |
orderByType | 排序方式:asc(默认)、desc | string | 否 |
accessToken | 分享访问令牌 | string | 否 |
返回值说明
HTTP 状态码:200,返回 contents(name、spaceId、type(file/dir)、size、updateTime、canPreview、canDownload、canSaveToNetdisk、inode)、marker、hasMore。
预览或下载分享文件
功能说明
PreviewShareFile 与 DownloadShareFile 分别用于预览和下载分享中的文件,均通过 HTTP 302 重定向到实际地址,SDK 不解析 JSON 响应体。调用前需先通过「验证提取码」获取分享访问令牌;预览/下载次数受分享配置的 previewLimit/downloadLimit 限制并计入 previewUsed/downloadUsed。使用示例
ctx := context.Background()// 预览分享文件httpRes, err := apiClient.ShareAPI.PreviewShareFile(ctx, "share-code", "file-inode").Preview(1).AccessToken(shareAccessToken).Execute()if err != nil {panic(err)}fmt.Printf("Preview status: %d\\n", httpRes.StatusCode)// 下载分享文件httpRes2, err := apiClient.ShareAPI.DownloadShareFile(ctx, "share-code", "file-inode").Download(1).AccessToken(shareAccessToken).Execute()if err != nil {panic(err)}fmt.Printf("Download status: %d\\n", httpRes2.StatusCode)
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
shareCode | 分享码 | string | 是 |
inodes | 文件 inode 链,末位必须指向文件 | string | 是 |
preview / download | 固定值 1,分别表示预览或下载 | int32 | 是 |
internalDomain | 是否使用内网域名,0 或 1 | int32 | 否 |
accessToken | 分享访问令牌 | string | 否 |
转存分享文件
功能说明
SaveShareFile 用于将分享中的文件转存到自己媒体库的指定空间,需分享配置开启 canSaveToNetdisk。转存可能同步完成(200)、转为异步任务(202)或部分成功(207)。使用示例
ctx := context.Background()saveReq := client.SaveShareFileRequest{TargetSpaceId: "your-space-id",TargetPath: client.PtrString("/saved/"),SourceInodesPath: "/",Inodes: []string{"file-inode-1"},ConflictResolutionStrategy: client.PtrString("rename"),}resp200, resp202, resp207, httpRes, err := apiClient.ShareAPI.SaveShareFile(ctx, "share-code").Save(1).SaveShareFileRequest(saveReq).AccessToken(shareAccessToken).Execute()if err != nil {panic(err)}switch httpRes.StatusCode {case 200:for _, item := range resp200.Result {fmt.Printf("status=%d path=%v\\n", *item.Status, item.Path)}case 202:fmt.Printf("异步转存任务,taskId: %d\\n", *resp202.TaskId)case 207:fmt.Println("部分成功")for _, item := range resp207.Result {fmt.Printf("status=%d path=%v\\n", *item.Status, item.Path)}}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
shareCode | 分享码 | string | 是 |
save | 固定值 1,表示转存分享文件 | int32 | 是 |
saveShareFileRequest | 转存请求对象 | SaveShareFileRequest | 是 |
accessToken | 分享访问令牌 | string | 否 |
SaveShareFileRequest 对象说明
字段 | 参数描述 | 类型 | 是否必填 |
TargetSpaceId | 转存目标空间 ID | string | 是 |
TargetPath | 转存目标路径 | *string | 否 |
SourceInodesPath | 分享中的源目录路径 | string | 是 |
Inodes | 转存的文件或目录 inode 数组,最多 1000 个 | []string | 是 |
ConflictResolutionStrategy | 冲突处理:ask、rename、overwrite;目标为目录时默认 ask 且不支持 overwrite,目标为文件时默认 rename | *string | 否 |