最近更新时间:2025-08-20 20:02:36
以下代码用于设置存储空间事件通知规则:
public void putBucketNotification() {
// 初始化 Ks3Client,参见“初始化”文档
Ks3Client ks3Client = initKs3Client();
try {
// 初始化事件通知规则
BucketNotification.NotificationRule rule = new BucketNotification.NotificationRule();
// 设置规则名称
rule.setRuleId("yourRuleId");
// 设置事件类型
rule.setEvents(Arrays.asList("ks3:ObjectCreated:PutObject", "ks3:ObjectCreated:PostObject"));
// 添加监听事件的资源
rule.addResource(new BucketNotification.Resource("your-prefix-1", "your-suffix-1"));
rule.addResource(new BucketNotification.Resource("your-prefix-2", "your-suffix-2"));
// 添加通知目标
BucketNotification.Destination destination = new BucketNotification.Destination();
destination.setDestType(BucketNotification.Destination.DEST_TYPE_ENDPOINT);
destination.setCallbackUrl("http://yourCallbackUrl");
rule.addDestination(destination);
// 设置通知报告,目前仅支持推送失败的报告
rule.setReport(new BucketNotification.Report("dst-bucket", "dst-prefix"));
// 初始化事件通知配置
BucketNotification bucketNotification = new BucketNotification();
// 添加事件通知规则,可以添加多个规则
bucketNotification.addNotification(rule);
// 设置事件通知
PutBucketNotificationRequest request = new PutBucketNotificationRequest("yourBucketName", bucketNotification);
ks3Client.putBucketNotification(request);
} 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();
}
}以下代码用于获取存储空间事件通知规则:
public void getBucketNotification() {
// 初始化 Ks3Client,参见“初始化”文档
Ks3Client ks3Client = initKs3Client();
try {
// 获取事件通知配置
BucketNotification bucketNotification = ks3Client.getBucketNotification("yourBucketName");
System.out.println("Bucket Notification: " + bucketNotification.toJson());
} 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();
}
}以下代码用于删除存储空间事件通知规则:
public void deleteBucketNotification() {
// 初始化 Ks3Client,参见“初始化”文档
Ks3Client ks3Client = initKs3Client();
try {
// 删除事件通知配置
ks3Client.deleteBucketNotification("yourBucketName");
} 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();
}
}
纯净模式
