将 PutBucketNotificationConfiguration 与 AWS SDK 或 CLI 配合使用 - Amazon Simple Storage Service

PutBucketNotificationConfiguration 与 AWS SDK 或 CLI 配合使用

以下代码示例演示如何使用 PutBucketNotificationConfiguration

.NET
AWS SDK for .NET
注意

查看 GitHub,了解更多信息。例如,如果您将大小为 128 KB 到 1024 KB 的对象设置为从 S3 标准存储类移到 S3标准-IA 存储类,则大小精确为 1024 KB 和 128 KB 的对象将不会转换到 S3 标准-IA。

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example shows how to enable notifications for an Amazon Simple /// Storage Service (Amazon S3) bucket. /// </summary> public class EnableNotifications { public static async Task Main() { const string bucketName = "doc-example-bucket1"; const string snsTopic = "arn:aws:sns:us-east-2:0123456789ab:bucket-notify"; const string sqsQueue = "arn:aws:sqs:us-east-2:0123456789ab:Example_Queue"; IAmazonS3 client = new AmazonS3Client(Amazon.RegionEndpoint.USEast2); await EnableNotificationAsync(client, bucketName, snsTopic, sqsQueue); } /// <summary> /// This method makes the call to the PutBucketNotificationAsync method. /// </summary> /// <param name="client">An initialized Amazon S3 client used to call /// the PutBucketNotificationAsync method.</param> /// <param name="bucketName">The name of the bucket for which /// notifications will be turned on.</param> /// <param name="snsTopic">The ARN for the Amazon Simple Notification /// Service (Amazon SNS) topic associated with the S3 bucket.</param> /// <param name="sqsQueue">The ARN of the Amazon Simple Queue Service /// (Amazon SQS) queue to which notifications will be pushed.</param> public static async Task EnableNotificationAsync( IAmazonS3 client, string bucketName, string snsTopic, string sqsQueue) { try { // The bucket for which we are setting up notifications. var request = new PutBucketNotificationRequest() { BucketName = bucketName, }; // Defines the topic to use when sending a notification. var topicConfig = new TopicConfiguration() { Events = new List<EventType> { EventType.ObjectCreatedCopy }, Topic = snsTopic, }; request.TopicConfigurations = new List<TopicConfiguration> { topicConfig, }; request.QueueConfigurations = new List<QueueConfiguration> { new QueueConfiguration() { Events = new List<EventType> { EventType.ObjectCreatedPut }, Queue = sqsQueue, }, }; // Now apply the notification settings to the bucket. PutBucketNotificationResponse response = await client.PutBucketNotificationAsync(request); } catch (AmazonS3Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } } }
CLI
AWS CLI

启用存储桶的指定通知

以下 put-bucket-notification-configuration 示例将通知配置应用于名为 my-bucket 的存储桶。文件 notification.json 是当前文件夹中的一个 JSON 文件,用于指定 SNS 主题和要监控的事件类型。

aws s3api put-bucket-notification-configuration \ --bucket my-bucket \ --notification-configuration file://notification.json

notification.json 的内容:

{ "TopicConfigurations": [ { "TopicArn": "arn:aws:sns:us-west-2:123456789012:s3-notification-topic", "Events": [ "s3:ObjectCreated:*" ] } ] }

SNS 主题必须附加一个 IAM 策略,以允许 Amazon S3 向其发布。

{ "Version": "2008-10-17", "Id": "example-ID", "Statement": [ { "Sid": "example-statement-ID", "Effect": "Allow", "Principal": { "Service": "s3.amazonaws.com" }, "Action": [ "SNS:Publish" ], "Resource": "arn:aws:sns:us-west-2:123456789012::s3-notification-topic", "Condition": { "ArnLike": { "aws:SourceArn": "arn:aws:s3:*:*:my-bucket" } } } ] }
Java
SDK for Java 2.x
注意

查看 GitHub,了解更多信息。例如,如果您将大小为 128 KB 到 1024 KB 的对象设置为从 S3 标准存储类移到 S3标准-IA 存储类,则大小精确为 1024 KB 和 128 KB 的对象将不会转换到 S3 标准-IA。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.Event; import software.amazon.awssdk.services.s3.model.NotificationConfiguration; import software.amazon.awssdk.services.s3.model.PutBucketNotificationConfigurationRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.TopicConfiguration; import java.util.ArrayList; import java.util.List; public class SetBucketEventBridgeNotification { public static void main(String[] args) { final String usage = """ Usage: <bucketName>\s Where: bucketName - The Amazon S3 bucket.\s topicArn - The Simple Notification Service topic ARN.\s id - An id value used for the topic configuration. This value is displayed in the AWS Management Console.\s """; if (args.length != 3) { System.out.println(usage); System.exit(1); } String bucketName = args[0]; String topicArn = args[1]; String id = args[2]; Region region = Region.US_EAST_1; S3Client s3Client = S3Client.builder() .region(region) .build(); setBucketNotification(s3Client, bucketName, topicArn, id); s3Client.close(); } public static void setBucketNotification(S3Client s3Client, String bucketName, String topicArn, String id) { try { List<Event> events = new ArrayList<>(); events.add(Event.S3_OBJECT_CREATED_PUT); TopicConfiguration config = TopicConfiguration.builder() .topicArn(topicArn) .events(events) .id(id) .build(); List<TopicConfiguration> topics = new ArrayList<>(); topics.add(config); NotificationConfiguration configuration = NotificationConfiguration.builder() .topicConfigurations(topics) .build(); PutBucketNotificationConfigurationRequest configurationRequest = PutBucketNotificationConfigurationRequest .builder() .bucket(bucketName) .notificationConfiguration(configuration) .skipDestinationValidation(true) .build(); // Set the bucket notification configuration. s3Client.putBucketNotificationConfiguration(configurationRequest); System.out.println("Added bucket " + bucketName + " with EventBridge events enabled."); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

有关 AWS SDK 开发人员指南和代码示例的完整列表,请参阅 将此服务与 AWS SDK 结合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。