最近在使用华为云SFS时,如果一个目录存储文件数超过100W,执行 "rm -rf path"时,存在删不动的情况,可以使用华为云API接口,执行异步删除。
华为官网:
实现代码:
java
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 = "<Your AK>";
String sk = "<Your SK>";
String pathStr = "<Your Path>";
String shareId = "<文件系统ID>";
// 删除
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());
}
}
}