华为云NFS使用API删除大文件目录

最近在使用华为云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 = "<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());
        }

    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值