Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Gunakan PutBucketNotificationConfigurationdengan AWS SDK atau CLI
Contoh kode berikut menunjukkan cara menggunakanPutBucketNotificationConfiguration.
Contoh tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Anda dapat melihat tindakan ini dalam konteks dalam contoh kode berikut:
- .NET
-
- SDK untuk .NET
-
Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS.
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 = "amzn-s3-demo-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
-
Untuk mengaktifkan notifikasi yang ditentukan ke bucket
put-bucket-notification-configurationContoh berikut menerapkan konfigurasi notifikasi ke bucket bernamaamzn-s3-demo-bucket. File tersebut notification.json adalah dokumen JSON di folder saat ini yang menentukan topik SNS dan jenis acara untuk dipantau.
aws s3api put-bucket-notification-configuration \
--bucket amzn-s3-demo-bucket \
--notification-configuration file://notification.json
Isi dari notification.json:
{
"TopicConfigurations": [
{
"TopicArn": "arn:aws:sns:us-west-2:123456789012:s3-notification-topic",
"Events": [
"s3:ObjectCreated:*"
]
}
]
}
Topik SNS harus memiliki kebijakan IAM yang melekat padanya yang memungkinkan Amazon S3 untuk mempublikasikannya.
{
"Version":"2012-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:*:*:amzn-s3-demo-bucket"
}
}
}
]
}
Untuk daftar lengkap panduan pengembang AWS SDK dan contoh kode, lihatMengembangkan dengan Amazon S3 menggunakan AWS SDK. Topik ini juga mencakup informasi tentang memulai dan detail tentang versi SDK sebelumnya.