对象存储服务 OBS-设置桶清单规则(Java SDK):代码示例

时间:2025-02-12 15:02:49

代码示例

本示例将为example-bucket桶设置桶清单,桶清单会统计前缀为exampleObjectPrefix的对象的信息。清单文件将存储在example-target-bucket桶中,清单文件的前缀为exampleInventoryPrefix。

 1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
import com.obs.services.ObsClient;import com.obs.services.exception.ObsException;import com.obs.services.model.HeaderResponse;import com.obs.services.model.inventory.InventoryConfiguration;import com.obs.services.model.inventory.SetInventoryConfigurationRequest;public class SetInventoryConfiguration001{    public static void main(String[] args) {        // 您可以通过环境变量获取访问密钥AK/SK,也可以使用其他外部引入方式传入。如果使用硬编码可能会存在泄露风险。        // 您可以登录访问管理控制台获取访问密钥AK/SK        String ak = System.getenv("AC CES S_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, 此处以华北-北京四为例,其他地区请按实际情况填写。        String endPoint = "https://obs.cn-north-4.myhuaweicloud.com";        // 您可以通过环境变量获取endPoint,也可以使用其他外部引入方式传入。        // String endPoint = System.getenv("ENDPOINT");                // 创建ObsClient实例        // 使用永久AK/SK初始化客户端        ObsClient obsClient = new ObsClient(ak, sk,endPoint);        // 使用临时AK/SK和SecurityToken初始化客户端        // ObsClient obsClient = new ObsClient(ak, sk, securityToken, endPoint);        try {            // 设置相关示例参数            String exampleBucketName = "example-bucket";            String exampleTargetBucketName = "example-target-bucket";            String exampleConfigurationId = "exampleConfigId001";            String exampleInventoryPrefix = "exampleInventoryPrefix";            String exampleObjectPrefix = "exampleObjectPrefix";            // 设置桶清单配置规则详细参数            InventoryConfiguration exampleConfiguration = new InventoryConfiguration();            exampleConfiguration.setDestinationBucket(exampleTargetBucketName);            exampleConfiguration.setConfigurationId(exampleConfigurationId);            exampleConfiguration.setInventoryFormat(InventoryConfiguration.InventoryFormatOptions. CS V);            exampleConfiguration.setFrequency(InventoryConfiguration.FrequencyOptions.DAILY);            exampleConfiguration.setEnabled(true);            exampleConfiguration.setIncludedObjectVersions(                    InventoryConfiguration.IncludedObjectVersionsOptions.CURRENT);            exampleConfiguration.setInventoryPrefix(exampleInventoryPrefix);            exampleConfiguration.setObjectPrefix(exampleObjectPrefix);            // 设置清单文件中会包含的额外的对象元数据字段            exampleConfiguration                    .getOptionalFields()                    .add(InventoryConfiguration.OptionalFieldOptions.IS_MULTIPART_UPLOADED);            exampleConfiguration.getOptionalFields().add(InventoryConfiguration.OptionalFieldOptions.ETAG);            exampleConfiguration                    .getOptionalFields()                    .add(InventoryConfiguration.OptionalFieldOptions.REPLICATION_STATUS);            SetInventoryConfigurationRequest request =                    new SetInventoryConfigurationRequest(exampleBucketName, exampleConfiguration);            // 设置桶清单配置规则            HeaderResponse response = obsClient.setInventoryConfiguration(request);            System.out.println("SetInventoryConfiguration succeeded");            System.out.println("HTTP Code: " + response.getStatusCode());        } catch (ObsException e) {            System.out.println("SetInventoryConfiguration 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("SetInventoryConfiguration failed");            // 其他异常信息打印            e.printStackTrace();        }    }}
support.huaweicloud.com/sdk-java-devg-obs/obs_21_0417.html