文件 AWS 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
-
- for PowerShell 工具
-
範例 1:此範例會設定 S3 event ObjectRemovedDelete 的 SNS 主題組態,並啟用指定 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 amzn-s3-demo-bucket -TopicConfiguration $topic
範例 2:此範例會針對傳送至 Lambda 函數的指定儲存貯體啟用 ObjectCreatedAll 通知。
$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 amzn-s3-demo-bucket -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 amzn-s3-demo-bucket -LambdaFunctionConfiguration $firstLambdaConfig,$secondlambdaConfig
-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 PutBucketNotification。
-
PutBucketLogging
PutBucketNotificationConfiguration