AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
与 AWS SDK或PutBucketNotification
一起使用 CLI
以下代码示例演示如何使用 PutBucketNotification
。
- CLI
-
- AWS CLI
-
将通知配置应用于名为
my-bucket
的存储桶:aws s3api put-bucket-notification --bucket
my-bucket
--notification-configurationfile://notification.json
该文件
notification.json
是当前文件夹中的一个JSON文档,用于指定要监视的SNS主题和事件类型:{ "TopicConfiguration": { "Event": "s3:ObjectCreated:*", "Topic": "arn:aws:sns:us-west-2:123456789012:s3-notification-topic" } }
该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:my-bucket", "Condition": { "ArnLike": { "aws:SourceArn": "arn:aws:s3:*:*:my-bucket" } } } ] }
-
有关API详细信息,请参阅AWS CLI 命令参考PutBucketNotification
中的。
-
- PowerShell
-
- 用于 PowerShell
-
示例 1:此示例为 S3 事件配置SNS主题配置 ObjectRemovedDelete 并启用给定 s3 存储桶的通知
$topic = [Amazon.S3.Model.TopicConfiguration] @{ Id = "delete-event" Topic = "arn:aws:sns:eu-west-1:123456789012:topic-1" Event = [Amazon.S3.EventType]::ObjectRemovedDelete } Write-S3BucketNotification -BucketName kt-tools -TopicConfiguration $topic
示例 2:此示例 ObjectCreatedAll 为给定存储桶启用通知,将其发送到 Lambda 函数。
$lambdaConfig = [Amazon.S3.Model.LambdaFunctionConfiguration] @{ Events = "s3:ObjectCreated:*" FunctionArn = "arn:aws:lambda:eu-west-1:123456789012:function:rdplock" Id = "ObjectCreated-Lambda" Filter = @{ S3KeyFilter = @{ FilterRules = @( @{Name="Prefix";Value="dada"} @{Name="Suffix";Value=".pem"} ) } } } Write-S3BucketNotification -BucketName ssm-editor -LambdaFunctionConfiguration $lambdaConfig
示例 3:此示例基于不同的键后缀创建 2 个不同的 Lambda 配置,并在单个命令中配置了这两个配置。
#Lambda Config 1 $firstLambdaConfig = [Amazon.S3.Model.LambdaFunctionConfiguration] @{ Events = "s3:ObjectCreated:*" FunctionArn = "arn:aws:lambda:eu-west-1:123456789012:function:verifynet" Id = "ObjectCreated-dada-ps1" Filter = @{ S3KeyFilter = @{ FilterRules = @( @{Name="Prefix";Value="dada"} @{Name="Suffix";Value=".ps1"} ) } } } #Lambda Config 2 $secondlambdaConfig = [Amazon.S3.Model.LambdaFunctionConfiguration] @{ Events = [Amazon.S3.EventType]::ObjectCreatedAll FunctionArn = "arn:aws:lambda:eu-west-1:123456789012:function:verifyssm" Id = "ObjectCreated-dada-json" Filter = @{ S3KeyFilter = @{ FilterRules = @( @{Name="Prefix";Value="dada"} @{Name="Suffix";Value=".json"} ) } } } Write-S3BucketNotification -BucketName ssm-editor -LambdaFunctionConfiguration $firstLambdaConfig,$secondlambdaConfig
-
有关API详细信息,请参阅 AWS Tools for PowerShell Cmdlet 参考PutBucketNotification中的。
-
PutBucketLogging
PutBucketNotificationConfiguration