对象存储服务 OBS-获取桶清单规则:代码示例

时间:2023-11-14 22:25:48

代码示例

import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.inventory.GetInventoryConfigurationRequest;
import com.obs.services.model.inventory.GetInventoryConfigurationResult;
import com.obs.services.model.inventory.InventoryConfiguration;

public class GetInventoryConfiguration001 {
    public static void main(String[] args) {
        // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量AC CES S_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");
        // 【可选】如果使用临时AK/SK和SecurityToken访问OBS,同样建议您尽量避免使用硬编码,以降低信息泄露风险。
        // 您可以通过环境变量获取访问密钥AK/SK/SecurityToken,也可以使用其他外部引入方式传入。
        String securityToken = System.getenv("SECURITY_TOKEN");

        // endpoint填写桶所在的endpoint, 此处以华北-北京四为例,其他地区请按实际情况填写。
        // 您可以通过环境变量获取endPoint,也可以使用其他外部引入方式传入。
        // String endPoint = System.getenv("ENDPOINT");
        String endPoint = "https://obs.cn-north-4.myhuaweicloud.com";

        // 创建ObsClient实例
        try (ObsClient obsClient = new ObsClient(ak, sk, securityToken, endPoint)) {
            // 设置相关示例参数
            String exampleBucketName = "example-bucket";
            String exampleConfigurationId = "exampleConfigId001";
            GetInventoryConfigurationRequest request =
                    new GetInventoryConfigurationRequest(exampleBucketName, exampleConfigurationId);
            // 获取桶清单配置规则
            GetInventoryConfigurationResult result = obsClient.getInventoryConfiguration(request);
            InventoryConfiguration inventoryConfiguration = result.getInventoryConfiguration();
            // 打印inventoryConfiguration的所有成员
            System.out.println("ConfigurationId:" + inventoryConfiguration.getConfigurationId());
            System.out.println("DestinationBucket:" + inventoryConfiguration.getDestinationBucket());
            System.out.println("InventoryFormat:" + inventoryConfiguration.getInventoryFormat());
            System.out.println("Enabled:" + inventoryConfiguration.getEnabled());
            System.out.println("Frequency:" + inventoryConfiguration.getFrequency());
            System.out.println("IncludedObjectVersions:" + inventoryConfiguration.getIncludedObjectVersions());
            System.out.println("InventoryPrefix:" + inventoryConfiguration.getInventoryPrefix());
            System.out.println("ObjectPrefix:" + inventoryConfiguration.getObjectPrefix());
            System.out.println("OptionalFields:" + inventoryConfiguration.getOptionalFields());
            // 打印HTTP状态码
            System.out.println("HTTP Code: " + result.getStatusCode());
            System.out.println("GetInventoryConfiguration succeeded");
        } catch (ObsException e) {
            System.out.println("GetInventoryConfiguration failed");
            // 请求失败,打印http状态码
            System.out.println("HTTP Code: " + e.getResponseCode());
            // 请求失败,打印服务端错误码
            System.out.println("Error Code:" + e.getErrorCode());
            // 请求失败,打印详细错误信息
            System.out.println("Error Message:" + e.getErrorMessage());
            // 请求失败,打印请求id
            System.out.println("Request ID:" + e.getErrorRequestId());
            System.out.println("Host ID:" + e.getErrorHostId());
        } catch (Exception e) {
            System.out.println("GetInventoryConfiguration failed");
            // 其他异常信息打印
            e.printStackTrace();
        }
    }
}
support.huaweicloud.com/sdk-java-devg-obs/zh-cn_topic_0000001715371177.html