最近在使用华为云SFS时,如果一个目录存储文件数超过100W,执行 “rm -rf path”时,存在删不动的情况,可以使用华为云API接口,执行异步删除。

华为官网:

删除文件系统目录_弹性文件服务 SFS_API参考_SFS Turbo API说明_目录管理_华为云删除文件系统目录在2023年6月1号之后创建的文件系统支持该API操作。 警告:该API为删除文件系统子目录数据的高危操作,删除后无法恢复,使用前请确认下发的文件系统目录是否正确。您可以在API Explorer中调试该接口,支持自动认证鉴权。API Explorer可以自动生成SDK代码示例,并提供SDK代码示例调试功能。DELETE https://support.huaweicloud.com/api-sfs/DeleteFsDir.html

实现代码:

import com.huaweicloud.sdk.core.auth.ICredential;import com.huaweicloud.sdk.core.auth.BasicCredentials;import com.huaweicloud.sdk.core.exception.ConnectionException;import com.huaweicloud.sdk.core.exception.RequestTimeoutException;import com.huaweicloud.sdk.core.exception.ServiceResponseException;import com.huaweicloud.sdk.sfsturbo.v1.region.SFSTurboRegion;import com.huaweicloud.sdk.sfsturbo.v1.*;import com.huaweicloud.sdk.sfsturbo.v1.model.*;/** * TODO 删除NFS文件系统目录,高危险操作,务必谨慎执行!!! * TODO 删除NFS文件系统目录,高危险操作,务必谨慎执行!!! * TODO 删除NFS文件系统目录,高危险操作,务必谨慎执行!!! * * * @author XHH */public class DeleteFsDirSolution {public static void main(String[] args) {String ak = "";String sk = "";String pathStr = "";String shareId = "";// 删除delFsDir(ak, sk, shareId, pathStr);}private static void delFsDir(String ak, String sk, String shareId, String pathStr) {ICredential auth = new BasicCredentials().withAk(ak).withSk(sk);SFSTurboClient client = SFSTurboClient.newBuilder().withCredential(auth).withRegion(SFSTurboRegion.valueOf("cn-southwest-2")).build();DeleteFsDirRequest request = new DeleteFsDirRequest();request.withShareId(shareId);DeleteFsDirRequestBody body = new DeleteFsDirRequestBody();body.withPath(pathStr);request.withBody(body);System.out.println("删除路径: " +pathStr);try {DeleteFsDirResponse response = client.deleteFsDir(request);System.out.println("response ===> " + response.toString());} catch (ConnectionException | RequestTimeoutException e) {e.printStackTrace();} catch (ServiceResponseException e) {e.printStackTrace();System.out.println("删除路径: " +pathStr+ " --> HttpStatusCode:" + e.getHttpStatusCode() + " --> RequestId:" + e.getRequestId() + " --> ErrorMsg:" + e.getErrorMsg());}}}