前期准备
开始操作前,确保您已经完成了 SDK 初始化。如果您还没有初始化 SDK,请先参考快速入门文档完成。
注意:
如果媒体库启用回收站功能,删除目录时会将目录及其下的文件移入回收站而非永久删除。
当目录内容较多时,复制操作会以异步方式执行,返回任务 ID。
列出目录内容
列出目录内容(marker 翻页,推荐)
listDirectory 用于获取指定目录下的所有文件和子目录,推荐使用 marker 方式翻页。目录内容的列出顺序为:首先按照字典序列出子目录,随后根据上传时间列出媒体库中的媒体资源,或根据文件名列出文件库中的文件资源。import com.tencent.cloud.smh.ApiException;import com.tencent.cloud.smh.ApiResponse;import com.tencent.cloud.smh.api.DirectoryApi;import com.tencent.cloud.smh.model.ListDirectory200Response;// 使用 marker/limit 分页查询(推荐)String nextMarker = null;try {DirectoryApi.APIListDirectoryRequest request = DirectoryApi.APIListDirectoryRequest.newBuilder().libraryId("your-library-id").spaceId("your-space-id").filePath("/images").byMarker(1).limit(20).orderBy("creationTime").orderByType("desc").filter("onlyFile").sortType("union").withInode(0).withFavoriteStatus(0).accessToken("your-access-token").userId("xxx").build();ApiResponse<Object> apiResponse = client.directory().listDirectoryWithHttpInfo(request);int statusCode = apiResponse.getStatusCode();System.out.println("Status code: " + statusCode);if (statusCode == 200) {ListDirectory200Response result = (ListDirectory200Response) apiResponse.getData();System.out.println("Directory contents: " + result.getContents().size() + " items");nextMarker = result.getNextMarker();if (nextMarker != null) {System.out.println("More data available, next marker: " + nextMarker);}}} catch (ApiException e) {System.err.println("Error: " + e.getCode() + " - " + e.getMessage());}// 获取下一页:将上一次响应中的 nextMarker 作为 marker 传入try {DirectoryApi.APIListDirectoryRequest request2 = DirectoryApi.APIListDirectoryRequest.newBuilder().libraryId("your-library-id").spaceId("your-space-id").filePath("/images").byMarker(1).marker(nextMarker).limit(20).accessToken("your-access-token").build();ApiResponse<Object> apiResponse2 = client.directory().listDirectoryWithHttpInfo(request2);// 处理响应...} catch (ApiException e) {System.err.println("Error: " + e.getCode() + " - " + e.getMessage());}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 目录路径,对于多级目录,使用斜杠(/)分隔,例如 foo/bar | String | 是 |
marker | 用于顺序列出分页的标识,不传/为空则默认第一页 | String | 否 |
limit | 用于顺序列出分页时本地列出的项目数限制,不传默认20,最大1000 | Integer | 否 |
orderBy | 排序字段,可选值:name、modificationTime、size、creationTime、localCreationTime | String | 否 |
orderByType | 排序方式,升序为 asc,降序为 desc | String | 否 |
filter | 筛选方式,不传返回全部,onlyDir 只返回文件夹,onlyFile 只返回文件 | String | 否 |
sortType | 排序方式,不传则文件和文件夹单独排序,先返回文件夹后返回文件。union 文件和文件夹拉通排序 | String | 否 |
withInode | 是否返回 inode(文件目录 ID),0或1,默认不返回 | Integer | 否 |
withFavoriteStatus | 是否返回收藏状态,0或1,默认不返回 | Integer | 否 |
accessToken | 访问令牌;对于公有读媒体库或租户空间可不指定,否则需通过本参数传入或提前调用 client.withToken() 注入,二者取一 | String | 否 |
userId | 用户身份识别,当访问令牌对应的权限为管理员权限且申请访问令牌时的用户身份识别为空时用来临时指定用户身份 | String | 否 |
返回值说明
HTTP 状态码:200,获取成功,返回目录内容列表。
字段 | 说明 | 类型 |
path | 当前请求的目录路径 | List<String> |
contents | 目录内容列表 | List<ListDirectory200ResponseContentsInner> |
nextMarker | 用于顺序列出分页的标识,为空表示已翻页完毕 | String |
contents 字段说明
字段 | 说明 | 类型 |
name | 目录或文件名 | String |
type | 条目类型 | String |
creationTime | 创建时间 | OffsetDateTime |
modificationTime | 修改时间 | OffsetDateTime |
contentType | 媒体类型 | String |
size | 文件大小 | String |
crc64 | CRC64校验值 | String |
fileType | 文件类型 | String |
path | 完整路径,数组最后一级为文件或目录名 | List<String> |
inode | 文件或目录 ID(withInode = 1时返回) | String |
versionId | 历史版本 ID,仅操作历史版本时返回 | Integer |
eTag | 文件 ETag | String |
isFavorite | 是否收藏(withFavoriteStatus = 1时返回) | Boolean |
metaData | 自定义元数据键值对 | Map<String, String> |
previewByDoc | 是否支持文档预览(WPS) | Boolean |
previewByCI | 是否支持数据万象预览 | Boolean |
previewAsIcon | 是否支持缩略图预览 | Boolean |
removedByQuota | 是否因配额超限被删除 | Boolean |
category | 文件自定义分类 | String |
labels | 简易标签列表 | List<String> |
localCreationTime | 文件本地创建时间,仅文件返回 | OffsetDateTime |
localModificationTime | 文件本地修改时间,仅文件返回 | OffsetDateTime |
contentCas | 内容 CAS 标识(withContentCas=1 时返回) | String |
列出目录内容(传统分页,不推荐)
listDirectoryByPage 用于获取指定目录下的所有文件和子目录,使用传统分页方式。不推荐使用,建议使用 marker 方式翻页。import com.tencent.cloud.smh.ApiException;import com.tencent.cloud.smh.ApiResponse;import com.tencent.cloud.smh.api.DirectoryApi;import com.tencent.cloud.smh.model.ListDirectoryByPage200Response;// 使用 page/pageSize 分页查询(不推荐)try {DirectoryApi.APIListDirectoryByPageRequest request = DirectoryApi.APIListDirectoryByPageRequest.newBuilder().libraryId("your-library-id").spaceId("your-space-id").filePath("/images").byPage(1).page(1).pageSize(20).orderBy("creationTime").orderByType("desc").filter("onlyFile").sortType("union").withInode(0).withFavoriteStatus(0).accessToken("your-access-token").userId("xxx").build();ApiResponse<Object> apiResponse = client.directory().listDirectoryByPageWithHttpInfo(request);int statusCode = apiResponse.getStatusCode();System.out.println("Status code: " + statusCode);if (statusCode == 200) {ListDirectoryByPage200Response result = (ListDirectoryByPage200Response) apiResponse.getData();System.out.println("File count: " + result.getFileCount());System.out.println("Sub-directory count: " + result.getSubDirCount());System.out.println("Total: " + result.getTotalNum());}} catch (ApiException e) {System.err.println("Error: " + e.getCode() + " - " + e.getMessage());}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 目录路径,对于多级目录,使用斜杠(/)分隔,例如 foo/bar | String | 是 |
page | 页码,不传默认1 | Integer | 否 |
pageSize | 每页数量,不传默认20;最大翻页条目数(Page×PageSize)为1万 | Integer | 否 |
orderBy | 排序字段,可选值:name、modificationTime、size、creationTime、localCreationTime | String | 否 |
orderByType | 排序方式,升序为 asc,降序为 desc | String | 否 |
filter | 筛选方式,不传返回全部,onlyDir 只返回文件夹,onlyFile 只返回文件 | String | 否 |
sortType | 排序方式,不传则文件和文件夹单独排序,先返回文件夹后返回文件。union 文件和文件夹拉通排序 | String | 否 |
withInode | 是否返回 inode(文件目录 ID),0或1,默认不返回 | Integer | 否 |
withFavoriteStatus | 是否返回收藏状态,0或1,默认不返回 | Integer | 否 |
accessToken | 访问令牌;对于公有读媒体库或租户空间可不指定,否则需通过本参数传入或提前调用 client.withToken() 注入,二者取一 | String | 否 |
userId | 用户身份识别,当访问令牌对应的权限为管理员权限且申请访问令牌时的用户身份识别为空时用来临时指定用户身份 | String | 否 |
返回值说明
HTTP 状态码:200,获取成功,返回目录内容列表。
字段 | 说明 | 类型 |
path | 当前请求的目录结构 | List<String> |
fileCount | 当前目录中的文件数 | Integer |
subDirCount | 当前目录中的子目录数 | Integer |
totalNum | 当前目录中的所有文件和子目录数量 | Integer |
contents | 目录内容列表 | List |
目录信息
查看目录详情
infoFileOrDirectory 用于获取指定路径的详细信息,可同时用于查看文件或文件夹详情。路径如果为文件,则返回文件详情,如果为文件夹,则返回文件夹详情。import com.tencent.cloud.smh.ApiException;import com.tencent.cloud.smh.ApiResponse;import com.tencent.cloud.smh.api.DirectoryApi;import com.tencent.cloud.smh.model.InfoFileOrDirectory200Response;import java.math.BigDecimal;// 获取目录详情try {DirectoryApi.APIInfoFileOrDirectoryRequest request = DirectoryApi.APIInfoFileOrDirectoryRequest.newBuilder().libraryId("your-library-id").spaceId("your-space-id").filePath("/documents").info(BigDecimal.ONE).withInode(1).withFavoriteStatus(1).accessToken("your-access-token").build();ApiResponse<Object> apiResponse = client.directory().infoFileOrDirectoryWithHttpInfo(request);int statusCode = apiResponse.getStatusCode();System.out.println("Status code: " + statusCode);if (statusCode == 200) {InfoFileOrDirectory200Response result = (InfoFileOrDirectory200Response) apiResponse.getData();System.out.println("Name: " + result.getName());System.out.println("Type: " + result.getType());System.out.println("Size: " + result.getSize());System.out.println("Inode: " + result.getInode());System.out.println("Is favorite: " + result.getIsFavorite());}} catch (ApiException e) {System.err.println("Error: " + e.getCode() + " - " + e.getMessage());}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 文件或目录路径 | String | 是 |
info | 固定值1,表示获取详细信息 | BigDecimal | 是 |
withInode | 是否返回 inode(文件目录 ID),0或1,默认不返回 | Integer | 否 |
withFavoriteStatus | 是否返回收藏状态,0或1,默认不返回 | Integer | 否 |
withContentCas | 是否返回 Cas 标识,0或1 | Integer | 否 |
accessToken | 访问令牌;对于公有读媒体库或租户空间可不指定,否则需通过本参数传入或提前调用 client.withToken() 注入,二者取一 | String | 否 |
返回值说明
HTTP 状态码:200,查询成功,返回文件或目录详情。
字段 | 说明 | 类型 |
path | 完整路径 | List<String> |
inode | 文件或目录 ID | String |
name | 文件或目录名 | String |
type | 条目类型(dir/file/image/video/symlink/virtual) | String |
userId | 创建人 ID | String |
creationTime | 创建时间 | OffsetDateTime |
modificationTime | 修改时间 | OffsetDateTime |
contentType | 媒体类型 | String |
size | 文件大小(仅文件返回) | String |
eTag | ETag | String |
isFavorite | 是否收藏 | Boolean |
crc64 | CRC64校验值 | String |
versionId | 版本号 | Integer |
metaData | 元数据 | Object |
labels | 标签列表 | List<String> |
category | 自定义分类 | String |
检查目录状态
checkDirectoryStatus 用于检查指定目录是否存在。import com.tencent.cloud.smh.ApiException;import com.tencent.cloud.smh.ApiResponse;import com.tencent.cloud.smh.api.DirectoryApi;try {DirectoryApi.APICheckDirectoryStatusRequest request = DirectoryApi.APICheckDirectoryStatusRequest.newBuilder().libraryId("your-library-id").spaceId("your-space-id").filePath("/documents/critical").accessToken("your-access-token").userId("xxx").build();ApiResponse<Object> apiResponse = client.directory().checkDirectoryStatusWithHttpInfo(request);System.out.println("Status code: " + apiResponse.getStatusCode());System.out.println("Directory status is OK");} catch (ApiException e) {System.err.println("Error: " + e.getCode() + " - " + e.getMessage());}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 目录路径 | String | 是 |
accessToken | 访问令牌;对于公有读媒体库或租户空间可不指定,否则需通过本参数传入或提前调用 client.withToken() 注入,二者取一 | String | 否 |
userId | 用户身份识别 | String | 否 |
返回值说明
HTTP 状态码:200,目录存在。
查询目录统计数据
getDirectoryStats 用于获取指定目录下的文件总大小、文件数量以及子目录数量,支持查询普通目录、回收站目录以及历史版本的统计量。可以查询普通目录的统计结果、回收站目录的统计结果、某个目录的历史版本的统计结果。
文件写操作和查询目录统计结果之间存在秒级时延,以最新查询结果为准。
import com.tencent.cloud.smh.ApiException;import com.tencent.cloud.smh.ApiResponse;import com.tencent.cloud.smh.api.DirectoryApi;import com.tencent.cloud.smh.model.GetDirectoryStats200Response;try {DirectoryApi.APIGetDirectoryStatsRequest request = DirectoryApi.APIGetDirectoryStatsRequest.newBuilder().libraryId("your-library-id").spaceId("your-space-id").filePath("/documents").stats(1).statsType("normal").accessToken("your-access-token").userId("user-id").build();ApiResponse<Object> apiResponse = client.directory().getDirectoryStatsWithHttpInfo(request);int statusCode = apiResponse.getStatusCode();System.out.println("Status code: " + statusCode);if (statusCode == 200) {GetDirectoryStats200Response result = (GetDirectoryStats200Response) apiResponse.getData();System.out.println("File count: " + result.getFileCount());System.out.println("Sub-directory count: " + result.getDirCount());System.out.println("Storage (bytes): " + result.getStorage());}} catch (ApiException e) {System.err.println("Error: " + e.getCode() + " - " + e.getMessage());}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID,如果媒体库为单租户模式,则该参数固定为连字符(-);如果媒体库为多租户模式,则必须指定该参数 | String | 是 |
filePath | 目录路径 | String | 是 |
stats | 固定值为1,表示查询目录统计数据 | Integer | 是 |
statsType | 统计类型,normal 表示普通目录统计量,recycle 表示回收站目录统计量,history 表示目录的历史版本统计量 | String | 是 |
recycledId | 回收站项目 ID,查询回收站的统计量时,为必选参数(根目录除外) | String | 否 |
accessToken | 访问令牌;对于公有读媒体库或租户空间可不指定,否则需通过本参数传入或提前调用 client.withToken() 注入,二者取一 | String | 否 |
userId | 用户身份识别,当访问令牌对应的权限为管理员权限且申请访问令牌时的用户身份识别为空时用来临时指定用户身份 | String | 否 |
返回值说明
HTTP 状态码:200,查询成功,返回目录统计信息。
响应示例:
{"userId": "user123","statsType": "normal","storage": 5558728615,"fileCount": 1024,"dirCount": 50}
响应字段说明:
字段 | 说明 | 类型 |
userId | 创建人 ID | String |
statsType | 查询类型 | String |
storage | 目录下所有文件总大小(字节),包含子目录文件;查询类型为历史版本时,为目录下所有文件历史版本总大小(字节) | Long |
fileCount | 目录下所有文件数量,包含子目录文件;查询类型为历史版本时,为目录下所有文件的历史版本个数 | Long |
dirCount | 目录下所有子目录数量;查询类型为历史版本时,该值始终为0 | Long |
修正目录统计数据
calibrateDirectoryStats 用于修正指定目录的统计数据,支持普通目录、回收站目录以及历史版本的统计量修正。修正操作以异步任务方式执行,返回任务 ID,可通过任务管理接口查询任务执行结果。该接口有调用频率限制,请勿频繁调用。import com.tencent.cloud.smh.ApiException;import com.tencent.cloud.smh.ApiResponse;import com.tencent.cloud.smh.api.DirectoryApi;import com.tencent.cloud.smh.model.CalibrateDirectoryStats200Response;try {DirectoryApi.APICalibrateDirectoryStatsRequest request = DirectoryApi.APICalibrateDirectoryStatsRequest.newBuilder().libraryId("your-library-id").spaceId("your-space-id").filePath("/documents").calibrate(1).statsType("normal").accessToken("your-access-token").userId("user-id").build();ApiResponse<Object> apiResponse = client.directory().calibrateDirectoryStatsWithHttpInfo(request);int statusCode = apiResponse.getStatusCode();if (statusCode == 200) {CalibrateDirectoryStats200Response result = (CalibrateDirectoryStats200Response) apiResponse.getData();System.out.println("Calibrate task submitted, taskId: " + result.getTaskId());// 后续通过任务管理接口轮询任务执行结果}} catch (ApiException e) {System.err.println("Error: " + e.getCode() + " - " + e.getMessage());}
参数说明:
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID,如果媒体库为单租户模式,则该参数固定为连字符(-);如果媒体库为多租户模式,则必须指定该参数 | String | 是 |
filePath | 目录路径 | String | 是 |
calibrate | 固定值为1,表示修正目录统计数据 | Integer | 是 |
statsType | 统计类型,normal 表示普通目录统计量,recycle 表示回收站目录统计量,history 表示目录的历史版本统计量 | String | 是 |
recycledId | 回收站项目 ID,查询回收站的统计量时,为必选参数(根目录除外) | String | 否 |
accessToken | 访问令牌;对于公有读媒体库或租户空间可不指定,否则需通过本参数传入或提前调用 client.withToken() 注入,二者取一 | String | 否 |
userId | 用户身份识别,当访问令牌对应的权限为管理员权限且申请访问令牌时的用户身份识别为空时用来临时指定用户身份 | String | 否 |
返回值说明:
HTTP 状态码:200,修正任务已提交,异步执行。
字段 | 说明 | 类型 |
taskId | 异步修正任务 ID,可通过任务管理接口查询任务执行结果 | Long |
目录管理
创建目录
createDirectory 用于在指定路径创建新的目录,会自动创建中间所需的各级父目录。import com.tencent.cloud.smh.ApiException;import com.tencent.cloud.smh.ApiResponse;import com.tencent.cloud.smh.api.DirectoryApi;import com.tencent.cloud.smh.model.CreateDirectory201Response;import com.tencent.cloud.smh.model.CreateDirectoryRequest;import java.time.OffsetDateTime;import java.util.List;import java.util.Map;// 设置请求体CreateDirectoryRequest createDirBody = new CreateDirectoryRequest();createDirBody.setMetaData(Map.of("department", "engineering", "project", "sdk-v2"));createDirBody.setLabels(List.of("重要", "项目文档"));createDirBody.setLocalCreationTime(OffsetDateTime.now());createDirBody.setLocalModificationTime(OffsetDateTime.now());try {DirectoryApi.APICreateDirectoryRequest request = DirectoryApi.APICreateDirectoryRequest.newBuilder().libraryId("your-library-id").spaceId("your-space-id").filePath("/project-docs").conflictResolutionStrategy("rename").accessToken("your-access-token").withInode(1).userId("xxx").createDirectoryRequest(createDirBody).build();ApiResponse<Object> apiResponse = client.directory().createDirectoryWithHttpInfo(request);int statusCode = apiResponse.getStatusCode();System.out.println("Status code: " + statusCode);if (statusCode == 201) {CreateDirectory201Response result = (CreateDirectory201Response) apiResponse.getData();System.out.println("Directory created, path: " + result.getPath());System.out.println("Inode: " + result.getInode());}} catch (ApiException e) {System.err.println("Error: " + e.getCode() + " - " + e.getMessage());}
参数说明:
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 目录路径 | String | 是 |
conflictResolutionStrategy | 最后一级目录冲突时的处理方式,ask: 冲突时返回 HTTP 409,rename: 冲突时自动重命名,默认为 ask | String | 否 |
withInode | 是否返回 inode(文件目录 ID),0或1,默认不返回 | Integer | 否 |
accessToken | 访问令牌;对于公有读媒体库或租户空间可不指定,否则需通过本参数传入或提前调用 client.withToken() 注入,二者取一 | String | 否 |
userId | 用户身份识别 | String | 否 |
createDirectoryRequest | 可选的请求体,用于指定目录的元数据信息 | CreateDirectoryRequest | 否 |
createDirectoryRequest 对象说明:
字段 | 参数描述 | 类型 | 是否必填 |
metaData | 自定义元数据键值对,key 为小写字符串 | Map<String, String> | 否 |
labels | 目录标签列表 | List<String> | 否 |
localCreationTime | 目录对应的本地创建时间 | OffsetDateTime | 否 |
localModificationTime | 目录对应的本地修改时间 | OffsetDateTime | 否 |
返回值说明:
HTTP 状态码:201,创建成功。
字段 | 说明 | 类型 |
path | 最终的目录或相簿路径,可能因自动重命名与指定路径不同 | List<String> |
inode | 最后一级文件目录 ID(withInode = 1时返回) | String |
creationTime | 目录创建时间 | OffsetDateTime |
metaData | 自定义元数据 | Map<String, String> |
labels | 标签列表 | List<String> |
localCreationTime | 本地创建时间 | OffsetDateTime |
localModificationTime | 本地修改时间 | OffsetDateTime |
复制目录
copyDirectory 用于将目录复制到目标路径,会自动创建中间所需的各级父目录。当目录内容较多时以异步方式复制。import com.tencent.cloud.smh.ApiException;import com.tencent.cloud.smh.ApiResponse;import com.tencent.cloud.smh.api.DirectoryApi;import com.tencent.cloud.smh.model.CopyDirectoryRequest;import com.tencent.cloud.smh.model.CopyDirectory200Response;import com.tencent.cloud.smh.model.CopyDirectory202Response;CopyDirectoryRequest copyBody = new CopyDirectoryRequest();copyBody.setCopyFrom("/source/xxx");try {DirectoryApi.APICopyDirectoryRequest request = DirectoryApi.APICopyDirectoryRequest.newBuilder().libraryId("your-library-id").spaceId("your-space-id").filePath("/dest/backup").conflictResolutionStrategy("ask").accessToken("your-access-token").userId("xxx").copyDirectoryRequest(copyBody).build();ApiResponse<Object> apiResponse = client.directory().copyDirectoryWithHttpInfo(request);int statusCode = apiResponse.getStatusCode();System.out.println("Status code: " + statusCode);if (statusCode == 200) {CopyDirectory200Response result = (CopyDirectory200Response) apiResponse.getData();System.out.println("Directory copied synchronously, path: " + result.getPath());} else if (statusCode == 202) {CopyDirectory202Response result = (CopyDirectory202Response) apiResponse.getData();System.out.println("Async copy accepted, taskId: " + result.getTaskId());// 需后续通过任务查询接口轮询进度}} catch (ApiException e) {System.err.println("Error: " + e.getCode() + " - " + e.getMessage());}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 目标目录路径 | String | 是 |
conflictResolutionStrategy | 最后一级目录冲突时的处理方式,ask 或 rename,默认为 ask | String | 否 |
accessToken | 访问令牌;对于公有读媒体库或租户空间可不指定,否则需通过本参数传入或提前调用 client.withToken() 注入,二者取一 | String | 否 |
userId | 用户身份识别 | String | 否 |
copyDirectoryRequest | 复制目录请求对象 | CopyDirectoryRequest | 是 |
copyDirectoryRequest 对象说明
字段 | 参数描述 | 类型 | 是否必填 |
copyFrom | 被复制的源目录或相簿路径 | String | 是 |
返回值说明
该接口返回多种 2xx 状态码,对应不同的响应体类型,调用方必须根据
apiResponse.getStatusCode() 判断后再进行类型强转:HTTP 状态码202:目录内容较多,以异步方式复制,返回 taskId。
HTTP 状态码204:同步复制成功(conflictResolutionStrategy 为 ask)。
HTTP 状态码200:同步复制成功(conflictResolutionStrategy 为 rename),返回最终路径。
HTTP 状态码 | 响应体类型 | 说明 |
200 | CopyDirectory200Response | 同步复制完成,响应体包含新目录路径等信息 |
202 | CopyDirectory202Response | 服务端已受理为异步任务(目录较大时),响应体包含任务 ID,需后续轮询任务状态 |
非 2xx 状态码(如 400、403、404、409 等)统一通过
ApiException 抛出。重命名或移动目录
moveDirectory 用于将目录移动到目标路径或重命名目录,可跨越多层级多目录。import com.tencent.cloud.smh.ApiException;import com.tencent.cloud.smh.ApiResponse;import com.tencent.cloud.smh.api.DirectoryApi;import com.tencent.cloud.smh.model.MoveDirectoryRequest;MoveDirectoryRequest moveBody = new MoveDirectoryRequest();moveBody.setFrom("/source/image/");try {DirectoryApi.APIMoveDirectoryRequest request = DirectoryApi.APIMoveDirectoryRequest.newBuilder().libraryId("your-library-id").spaceId("your-space-id").filePath("/dest/image/").conflictResolutionStrategy("ask").accessToken("your-access-token").userId("xxx").moveDirectoryRequest(moveBody).build();ApiResponse<Object> apiResponse = client.directory().moveDirectoryWithHttpInfo(request);int statusCode = apiResponse.getStatusCode();if (statusCode == 204) {System.out.println("Directory moved successfully");} else if (statusCode == 200) {System.out.println("Directory moved and renamed, path updated");}} catch (ApiException e) {System.err.println("Error: " + e.getCode() + " - " + e.getMessage());}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 目标目录路径 | String | 是 |
conflictResolutionStrategy | 最后一级目录冲突时的处理方式,ask 或 rename,默认为 ask | String | 否 |
accessToken | 访问令牌;对于公有读媒体库或租户空间可不指定,否则需通过本参数传入或提前调用 client.withToken() 注入,二者取一 | String | 否 |
userId | 用户身份识别 | String | 否 |
moveDirectoryRequest | 移动目录请求对象 | MoveDirectoryRequest | 是 |
moveDirectoryRequest 对象说明:
字段 | 参数描述 | 类型 | 是否必填 |
from | 源目录路径 | String | 是 |
返回值说明:
HTTP 状态码 204:同步移动成功(conflictResolutionStrategy 为 ask)。
HTTP 状态码 200:同步移动成功(conflictResolutionStrategy 为 rename),返回最终路径。
删除目录
deleteDirectory 用于删除指定目录及其下的所有文件。如果媒体库启用回收站功能,则移入回收站而非永久删除。import com.tencent.cloud.smh.ApiException;import com.tencent.cloud.smh.ApiResponse;import com.tencent.cloud.smh.api.DirectoryApi;// 移入回收站try {DirectoryApi.APIDeleteDirectoryRequest request = DirectoryApi.APIDeleteDirectoryRequest.newBuilder().libraryId("your-library-id").spaceId("your-space-id").filePath("/documents/old-folder").permanent(0).accessToken("your-access-token").userId("xxx").build();ApiResponse<Object> apiResponse = client.directory().deleteDirectoryWithHttpInfo(request);int statusCode = apiResponse.getStatusCode();if (statusCode == 200) {System.out.println("Directory moved to recycle bin");// 开启回收站时返回 recycledItemId} else if (statusCode == 204) {System.out.println("Directory permanently deleted");}} catch (ApiException e) {System.err.println("Error: " + e.getCode() + " - " + e.getMessage());}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 目录路径 | String | 是 |
permanent | 当媒体库开启回收站时,1: 永久删除,0: 移入回收站,默认为0 | Integer | 否 |
directoryOnly | 是否只删除目录本身而不级联删除目录下的内容,取值1或 true 表示只删除目录本身,可选参数 | String | 否 |
accessToken | 访问令牌;对于公有读媒体库或租户空间可不指定,否则需通过本参数传入或提前调用 client.withToken() 注入,二者取一 | String | 否 |
userId | 用户身份识别 | String | 否 |
返回值说明
HTTP 状态码 204:删除成功(未开启回收站)。
HTTP 状态码 200:删除成功(开启回收站),返回回收站项目 ID。
字段 | 说明 | 类型 |
recycledItemId | 回收站项目 ID | Long |
标签与分类
更新目录标签
updateDirectoryLabels 用于更新目录的标签信息。需要 admin 或 space_admin 权限。import com.tencent.cloud.smh.ApiException;import com.tencent.cloud.smh.ApiResponse;import com.tencent.cloud.smh.api.DirectoryApi;import com.tencent.cloud.smh.model.UpdateDirectoryLabelsRequest;import java.math.BigDecimal;import java.util.List;import java.util.Map;UpdateDirectoryLabelsRequest labelsBody = new UpdateDirectoryLabelsRequest();labelsBody.setLabels(List.of("tag1", "tag2", "important"));labelsBody.setMetaData(Map.of("department", "finance"));labelsBody.setMetaDataDirective(UpdateDirectoryLabelsRequest.MetaDataDirectiveEnum.MERGE);try {DirectoryApi.APIUpdateDirectoryLabelsRequest request = DirectoryApi.APIUpdateDirectoryLabelsRequest.newBuilder().libraryId("your-library-id").spaceId("your-space-id").filePath("/documents/important").accessToken("your-access-token").update(BigDecimal.ONE).updateDirectoryLabelsRequest(labelsBody).build();ApiResponse<Object> apiResponse = client.directory().updateDirectoryLabelsWithHttpInfo(request);int statusCode = apiResponse.getStatusCode();if (statusCode == 204) {System.out.println("Directory labels updated");}} catch (ApiException e) {System.err.println("Error: " + e.getCode() + " - " + e.getMessage());}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 目录路径 | String | 是 |
accessToken | 访问令牌;对于公有读媒体库或租户空间可不指定,否则需通过本参数传入或提前调用 client.withToken() 注入,二者取一 | String | 否 |
update | 固定值1,表示更新标签或分类操作 | BigDecimal | 是 |
updateDirectoryLabelsRequest | 更新目录标签请求对象 | UpdateDirectoryLabelsRequest | 否 |
updateDirectoryLabelsRequest 对象说明
字段 | 参数描述 | 类型 | 是否必填 |
labels | 目录标签列表 | List<String> | 否 |
metaData | 自定义元数据 | Map<String, String> | 否 |
metaDataDirective | 元数据更新策略,merge: 合并,replace: 替换 | String | 否 |
返回值说明
HTTP 状态码:204,更新成功,无响应体。
更新文件标签或分类
updateFileLabels 用于更新文件的标签(Labels)或分类(Category)。需要 admin 或 space_admin 权限。import com.tencent.cloud.smh.ApiException;import com.tencent.cloud.smh.ApiResponse;import com.tencent.cloud.smh.api.DirectoryApi;import com.tencent.cloud.smh.model.UpdateFileLabelsRequest;import java.math.BigDecimal;import java.time.OffsetDateTime;import java.util.List;import java.util.Map;UpdateFileLabelsRequest fileLabelsBody = new UpdateFileLabelsRequest();fileLabelsBody.setLabels(List.of("动物", "大象", "亚洲象"));fileLabelsBody.setCategory("image");fileLabelsBody.setMetaData(Map.of("source", "camera"));fileLabelsBody.setMetaDataDirective(UpdateFileLabelsRequest.MetaDataDirectiveEnum.MERGE);fileLabelsBody.setLocalCreationTime(OffsetDateTime.parse("2024-01-15T10:30:00+08:00"));fileLabelsBody.setLocalModificationTime(OffsetDateTime.parse("2024-01-15T10:30:00+08:00"));fileLabelsBody.setContentType("image/jpeg");fileLabelsBody.setSize("1024000");try {DirectoryApi.APIUpdateFileLabelsRequest request = DirectoryApi.APIUpdateFileLabelsRequest.newBuilder().libraryId("your-library-id").spaceId("your-space-id").filePath("text.txt").accessToken("your-access-token").update(BigDecimal.ONE).updateFileLabelsRequest(fileLabelsBody).build();ApiResponse<Object> apiResponse = client.directory().updateFileLabelsWithHttpInfo(request);int statusCode = apiResponse.getStatusCode();if (statusCode == 204) {System.out.println("File labels updated");}} catch (ApiException e) {System.err.println("Error: " + e.getCode() + " - " + e.getMessage());}
参数说明
参数名 | 参数描述 | 类型 | 是否必填 |
libraryId | 媒体库 ID | String | 是 |
spaceId | 空间 ID | String | 是 |
filePath | 文件路径 | String | 是 |
accessToken | 访问令牌;对于公有读媒体库或租户空间可不指定,否则需通过本参数传入或提前调用 client.withToken() 注入,二者取一 | String | 否 |
update | 固定值1,表示更新标签或分类操作 | BigDecimal | 是 |
updateFileLabelsRequest | 更新文件标签请求对象 | UpdateFileLabelsRequest | 是 |
updateFileLabelsRequest 对象说明
字段 | 参数描述 | 类型 | 是否必填 |
labels | 文件标签列表 | List<String> | 否 |
category | 文件自定义的分类,最大长度 16 字节 | String | 否 |
metaData | 自定义元数据 | Map<String, String> | 否 |
metaDataDirective | 元数据更新策略,merge: 合并,replace: 替换 | String | 否 |
localCreationTime | 文件对应的本地创建时间 | OffsetDateTime | 否 |
localModificationTime | 文件对应的本地修改时间 | OffsetDateTime | 否 |
contentType | 媒体类型 | String | 否 |
size | 虚拟文件大小(字节) | String | 否 |
返回值说明
HTTP 状态码:204,更新成功,无响应体。