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

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

代码示例

本示例用于删除example-bucket桶的exampleConfigId001清单规则。

 1 2 3 4 5 6 7 8 91011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
import com.obs.services.ObsClient;import com.obs.services.exception.ObsException;import com.obs.services.model.HeaderResponse;import com.obs.services.model.inventory.DeleteInventoryConfigurationRequest;import com.obs.services.model.inventory.GetInventoryConfigurationRequest;import com.obs.services.model.inventory.GetInventoryConfigurationResult;import com.obs.services.model.inventory.InventoryConfiguration;public class DeleteInventoryConfiguration001 {    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 exampleConfigurationId = "exampleConfigId001";            DeleteInventoryConfigurationRequest request =                    new DeleteInventoryConfigurationRequest(exampleBucketName, exampleConfigurationId);            // 删除桶清单配置规则            HeaderResponse result = obsClient.deleteInventoryConfiguration(request);            // 打印HTTP状态码            System.out.println("HTTP Code: " + result.getStatusCode());            System.out.println("DeleteInventoryConfiguration succeeded");        } catch (ObsException e) {            System.out.println("DeleteInventoryConfiguration 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("DeleteInventoryConfiguration failed");            // 其他异常信息打印            e.printStackTrace();        }    }}
support.huaweicloud.com/sdk-java-devg-obs/obs_21_0420.html