生命周期

最后更新时间:2020-08-10 10:24:49

    简介

    本文档提供关于生命周期的 API 概览以及 SDK 示例代码。

    API 操作名 操作描述
    PUT Bucket lifecycle 设置生命周期 设置存储桶的生命周期管理的配置
    GET Bucket lifecycle 查询生命周期 查询存储桶生命周期管理的配置
    DELETE Bucket lifecycle 删除生命周期 删除存储桶生命周期管理的配置

    SDK API 参考

    SDK 所有接口的具体参数与方法说明,请参考 SDK API 参考

    设置生命周期

    功能说明

    设置指定存储桶的生命周期配置信息。

    示例代码

    String bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
    PutBucketLifecycleRequest putBucketLifecycleRequest =
            new PutBucketLifecycleRequest(bucket);
    
    // 声明周期配置规则信息
    LifecycleConfiguration.Rule rule = new LifecycleConfiguration.Rule();
    rule.id = "Lifecycle ID";
    LifecycleConfiguration.Filter filter = new LifecycleConfiguration.Filter();
    // 指定规则所适用的前缀
    filter.prefix = "dir/";
    rule.filter = filter;
    // 指明规则是否启用
    rule.status = "Enabled";
    // 指明规则对应的动作在对象最后的修改日期过后多少天操作
    LifecycleConfiguration.Transition transition =
            new LifecycleConfiguration.Transition();
    transition.days = 100;
    transition.storageClass = COSStorageClass.STANDARD.getStorageClass();
    rule.transition = transition;
    
    putBucketLifecycleRequest.setRuleList(rule);
    
    cosXmlService.putBucketLifecycleAsync(putBucketLifecycleRequest,
            new CosXmlResultListener() {
        @Override
        public void onSuccess(CosXmlRequest request, CosXmlResult result) {
            PutBucketLifecycleResult putBucketLifecycleResult =
                    (PutBucketLifecycleResult) result;
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    说明:

    更多完整示例,请前往 GitHub 查看。

    查询生命周期

    功能说明

    查询存储桶的生命周期管理配置。

    示例代码

    String bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
    GetBucketLifecycleRequest getBucketLifecycleRequest =
            new GetBucketLifecycleRequest(bucket);
    
    cosXmlService.getBucketLifecycleAsync(getBucketLifecycleRequest,
            new CosXmlResultListener() {
        @Override
        public void onSuccess(CosXmlRequest request, CosXmlResult result) {
            GetBucketLifecycleResult getBucketLifecycleResult =
                    (GetBucketLifecycleResult) result;
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    说明:

    更多完整示例,请前往 GitHub 查看。

    删除生命周期

    功能说明

    删除存储桶生命周期管理的配置。

    示例代码

    String bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
    DeleteBucketLifecycleRequest deleteBucketLifecycleRequest =
            new DeleteBucketLifecycleRequest(bucket);
    
    cosXmlService.deleteBucketLifecycleAsync(deleteBucketLifecycleRequest,
            new CosXmlResultListener() {
        @Override
        public void onSuccess(CosXmlRequest request, CosXmlResult result) {
            DeleteBucketLifecycleResult deleteBucketLifecycleResult =
                    (DeleteBucketLifecycleResult) result;
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    说明:

    更多完整示例,请前往 GitHub 查看。

    Was this page helpful?

    本页内容是否解决了您的问题?

    • 完全没帮助
    • 文档较差
    • 文档一般
    • 文档不错
    • 文档很好
    反馈
    帮助