最近更新时间:2025-11-07 15:09:34
以下代码用于设置VPC访问控制规则:
public void putVpcAccessBlock() {
// 初始化 Ks3Client, 参见“初始化”文档
Ks3Client ks3Client = initKs3Client();
try {
// 创建 VPC 访问控制规则
VpcAccessBlockConfiguration.Rule rule1 = new VpcAccessBlockConfiguration.Rule();
// 规则 ID,唯一标识
rule1.setId("rule1");
// 不允许访问该 Region 资源的 VPC ID 列表
rule1.setVpcIds(Arrays.asList("vpc-1", "vpc-2"));
// vpc 所在 region
rule1.setRegion("cn-beijing-6");
// 允许访问的 Bucket 名称列表,为空表示所有 Bucket 都不允许访问
rule1.setAllowAccessBuckets(Arrays.asList("bucket1", "bucket2"));
// 是否启用该规则
rule1.setEnabled(true);
VpcAccessBlockConfiguration.Rule rule2 = new VpcAccessBlockConfiguration.Rule();
rule2.setId("rule1");
rule2.setVpcIds(Arrays.asList("vpc-3", "vpc-4"));
rule1.setRegion("cn-beijing-6");
rule2.setAllowAccessBuckets(null);
rule2.setEnabled(true);
VpcAccessBlockConfiguration config = new VpcAccessBlockConfiguration();
config.addRule(rule1);
config.addRule(rule2);
ks3Client.putVpcAccessBlock(new PutVpcAccessBlockRequest(config));
} catch (Ks3ServiceException e) {
System.out.println("Ks3ServiceException occurred, which means the request was made to KS3, but KS3 returned an error response for some reason.");
System.out.println("Error Message: " + e.getErrorMessage());
System.out.println("Error Code: " + e.getErrorCode());
System.out.println("Request ID: " + e.getRequestId());
} catch (Ks3ClientException e) {
System.out.println("Ks3ClientException occurred, which means the client encountered an internal error while trying to communicate with KS3.");
System.out.println("Error Message: " + e.getMessage());
} finally {
// 不再使用时,关闭 Ks3Client
ks3Client.shutdown();
}
}以下代码用于获取VPC访问控制规则:
public void getVpcAccessBlock() {
// 初始化 Ks3Client, 参见“初始化”文档
Ks3Client ks3Client = initKs3Client();
try {
VpcAccessBlockConfiguration config = ks3Client.getVpcAccessBlock();
for (VpcAccessBlockConfiguration.Rule rule : config.getRules()) {
System.out.println("规则 ID: " + rule.getId());
System.out.println("不允许访问该 Region 资源的 VPC ID 列表: " + rule.getVpcIds());
System.out.println("VPC 所在 region: " + rule.getRegion());
System.out.println("允许访问的 Bucket 名称列表: " + rule.getAllowAccessBuckets());
System.out.println("是否启用该规则: " + rule.isEnabled());
}
} catch (Ks3ServiceException e) {
System.out.println("Ks3ServiceException occurred, which means the request was made to KS3, but KS3 returned an error response for some reason.");
System.out.println("Error Message: " + e.getErrorMessage());
System.out.println("Error Code: " + e.getErrorCode());
System.out.println("Request ID: " + e.getRequestId());
} catch (Ks3ClientException e) {
System.out.println("Ks3ClientException occurred, which means the client encountered an internal error while trying to communicate with KS3.");
System.out.println("Error Message: " + e.getMessage());
} finally {
// 不再使用时,关闭 Ks3Client
ks3Client.shutdown();
}
}以下代码用于删除VPC访问控制规则:
public void deleteVpcAccessBlock() {
// 初始化 Ks3Client, 参见“初始化”文档
Ks3Client ks3Client = initKs3Client();
try {
ks3Client.deleteVpcAccessBlock();
} catch (Ks3ServiceException e) {
System.out.println("Ks3ServiceException occurred, which means the request was made to KS3, but KS3 returned an error response for some reason.");
System.out.println("Error Message: " + e.getErrorMessage());
System.out.println("Error Code: " + e.getErrorCode());
System.out.println("Request ID: " + e.getRequestId());
} catch (Ks3ClientException e) {
System.out.println("Ks3ClientException occurred, which means the client encountered an internal error while trying to communicate with KS3.");
System.out.println("Error Message: " + e.getMessage());
} finally {
// 不再使用时,关闭 Ks3Client
ks3Client.shutdown();
}
}
纯净模式
