更新时间:2024-06-21 GMT+08:00
分享

查看生命周期规则

开发过程中,您有任何问题可以在github上提交issue,或者在华为云对象存储服务论坛中发帖求助。接口参考文档详细介绍了每个接口的参数和使用方法。

您可以通过ObsClient.getBucketLifecycle查看桶的生命周期规则。以下代码展示了如何查看桶的生命周期规则:

// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY_ID。
// 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// 创建ObsClient实例
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

LifecycleConfiguration config = obsClient.getBucketLifecycle("bucketname");

for (LifecycleConfiguration.Rule rule : config.getRules()) {
    Log.i("GetBucketLifecycleConfiguration",rule.getId());
    Log.i("GetBucketLifecycleConfiguration",rule.getPrefix());
    for(LifecycleConfiguration.Transition transition : rule.getTransitions()){
        Log.i("GetBucketLifecycleConfiguration",String.valueOf(transition.getDays()));
        Log.i("GetBucketLifecycleConfiguration",transition.getStorageClass());
    }
    Log.i("GetBucketLifecycleConfiguration",rule.getExpiration() != null ? String.valueOf(rule.getExpiration().getDays()) : "");
    for(LifecycleConfiguration.NoncurrentVersionTransition noncurrentVersionTransition : rule.getNoncurrentVersionTransitions()){
        Log.i("GetBucketLifecycleConfiguration",String.valueOf(noncurrentVersionTransition.getDays()));
        Log.i("GetBucketLifecycleConfiguration",noncurrentVersionTransition.getStorageClass());
    }
    Log.i("GetBucketLifecycleConfiguration",rule.getNoncurrentVersionExpiration() != null ?String.valueOf(rule.getNoncurrentVersionExpiration().getDays()) : "");
}

相关文档