將 S3 事件通知傳送至 EventBridge Amazon AWS SDK - Amazon Simple Storage Service

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

將 S3 事件通知傳送至 EventBridge Amazon AWS SDK

下列程式碼範例示範如何啟用儲存貯體將 S3 事件通知傳送至 Amazon 主題 EventBridge 和 Amazon 佇列,並將通知路由傳送至 Amazon SNS 主題和 Amazon SQS 佇列。

Java
SDK對於爪哇 2.x
注意

還有更多關於 GitHub。尋找完整範例,並瞭解如何在 AWS 代碼示例存儲庫

/** This method configures a bucket to send events to AWS EventBridge and creates a rule * to route the S3 object created events to a topic and a queue. * * @param bucketName Name of existing bucket * @param topicArn ARN of existing topic to receive S3 event notifications * @param queueArn ARN of existing queue to receive S3 event notifications * * An AWS CloudFormation stack sets up the bucket, queue, topic before the method runs. */ public static String setBucketNotificationToEventBridge(String bucketName, String topicArn, String queueArn) { try { // Enable bucket to emit S3 Event notifications to EventBridge. s3Client.putBucketNotificationConfiguration(b -> b .bucket(bucketName) .notificationConfiguration(b1 -> b1 .eventBridgeConfiguration( SdkBuilder::build) ).build()).join(); // Create an EventBridge rule to route Object Created notifications. PutRuleRequest putRuleRequest = PutRuleRequest.builder() .name(RULE_NAME) .eventPattern(""" { "source": ["aws.s3"], "detail-type": ["Object Created"], "detail": { "bucket": { "name": ["%s"] } } } """.formatted(bucketName)) .build(); // Add the rule to the default event bus. PutRuleResponse putRuleResponse = eventBridgeClient.putRule(putRuleRequest) .whenComplete((r, t) -> { if (t != null) { logger.error("Error creating event bus rule: " + t.getMessage(), t); throw new RuntimeException(t.getCause().getMessage(), t); } logger.info("Event bus rule creation request sent successfully. ARN is: {}", r.ruleArn()); }).join(); // Add the existing SNS topic and SQS queue as targets to the rule. eventBridgeClient.putTargets(b -> b .eventBusName("default") .rule(RULE_NAME) .targets(List.of ( Target.builder() .arn(queueArn) .id("Queue") .build(), Target.builder() .arn(topicArn) .id("Topic") .build()) ) ).join(); return putRuleResponse.ruleArn(); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return null; }

有關的完整列表 AWS SDK開發人員指南和代碼示例,請參閱搭配 AWS SDK 使用此服務。本主題也包含有關入門的資訊以及舊SDK版的詳細資訊。