You can configure event notification rules in the Object Storage Service (OSS) console to monitor specific objects. When a specified event occurs for an object, Object Storage Service (OSS) sends a notification to a Simple Message Queue (SMQ) queue.
Scenario
A company creates a bucket named srcbucket in the China (Hangzhou) region. This bucket contains log objects with the log/ prefix and customer case image objects with the destdir/ prefix.
srcbucket
└── log/
├── date1.txt
├── date2.txt
├── date3.txt
├── .......
└── destdir/
├── photo1.jpg
├── photo2.jpg
├── ......
A subsidiary needs to replicate new log and image objects from srcbucket in real time, including any creations, deletions, or modifications. The replication destination is a bucket named destbucket, which is owned by the parent company in the UK (London) region. The company also wants to notify all employees of object changes that match the log/ and destdir/ prefixes in both srcbucket and destbucket.
To meet these requirements, configure a cross-region replication (CRR) rule for srcbucket and set up event notifications for both srcbucket and destbucket.
Step 1: Create queues
-
Log on to the SMQ console.
In the left-side navigation pane, choose Queue Model > Queues.
-
In the top navigation bar, select the China (Hangzhou) region.
-
On the Queues page, click Create Queue. Set the queue name to myqueue1 and keep the default settings for the other parameters.
-
Click OK.
-
Repeat the preceding steps to create a queue named myqueue2 in the UK (London) region.
Step 2: Configure cross-region replication
Log on to the OSS console.
-
Click Buckets, and then click srcbucket.
-
In the left-side navigation pane, choose .
-
On the CRR page, click CRR.
-
In the CRR panel, configure the following parameters and click OK.
Parameter
Description
Destination Bucket
Select UK (London) as the region and destbucket as the bucket.
Objects to Replicate
Select Objects with Specified Prefix and add the prefixes destdir/ and log/.
Replication Policy
Select Add/Delete/Change Sync.
Replicate Historical Data
Select Copy.
RAM Role Name
Select the RAM role for cross-region replication.
Step 3: Configure event notifications
The CRR rule configured in Step 2 includes replicating historical data. If there is a large amount of historical data, this process can trigger a high volume of messages. To avoid this, enable event notifications only after the historical data replication is complete.
Log on to the OSS console.
-
Click Buckets. Follow these steps to configure event notifications for both srcbucket and destbucket:
-
In the left-side navigation pane, choose .
-
On the Event Notification page, click Create Rule.
-
In the Create Rule panel, configure the parameters as described in the following table, and then click OK.
Parameter
Source (srcbucket)
Destination (destbucket)
Rule Name
Set the rule name to notification1.
Set the rule name to notification2.
Event Type
Select PutObject, CopyObject, DeleteObject, and DeleteObjects.
Select ObjectReplication:ObjectCreated, ObjectReplication:ObjectRemoved, and ObjectReplication:ObjectModified.
Resource Description
Select Prefix and Suffix. Add the prefixes log/ and destdir/.
Select Prefix and Suffix. Add the prefixes log/ and destdir/.
Endpoint
Select Queue. For the queue name, enter myqueue1, which you created in Step 1.
Select Queue. For the queue name, enter myqueue2, which you created in Step 1.
-
The event notification rules take effect about 10 minutes after you configure them.
Step 4: Create subscriptions and receive messages
When a matching event occurs, SMQ automatically creates a topic. The topic name follows the format mns-en-topics-[Product]-[RuleName]-[Timestamp], for example, mns-en-topics-oss-notification1-1700098799. You must create a subscription for this topic and specify a receiver endpoint.
-
Navigate to the subscription page.
-
Log on to the SMQ console.
-
In the left-side navigation pane, click Topics.
-
In the top navigation bar, select the China (Hangzhou) region.
-
Find the topic that SMQ automatically created and click View Subscriptions in the Actions column.
-
-
Create a subscription.
-
On the topic details page, click the Subscriptions tab, and then click Create Subscription.
-
On the Create Subscription page, create a subscription for the source bucket, srcbucket. Set Subscription Name to mysubscription1, Push Type to Queue, and Receiver Endpoint to myqueue1. Keep the default settings for the other parameters.
-
Click OK.
-
-
Repeat these steps in the UK (London) region to create a subscription named mysubscription2 for the destination bucket, destbucket. Select Queue as the push type and specify myqueue2 as the receiver endpoint. Keep the default settings for the other parameters.
-
Receive notifications.
-
In the left-side navigation pane, click Queues.
-
In the Actions column for the target queue myqueue1, choose .
-
In the Receive Message section, click Receive Message in the upper-right corner.
You can now receive event notifications for the creation, modification, and deletion of objects that match the
log/anddestdir/prefixes in srcbucket. -
Repeat these steps for the queue myqueue2 to receive notifications from the destination bucket, destbucket. After the configuration is complete, you will receive event notifications when objects in destbucket are created, overwritten, or deleted by the CRR rule.
When you no longer need to receive event notifications, delete the corresponding event notification rules. Note that deleting a rule does not automatically delete the topic created for it. To avoid unnecessary charges, you must manually delete unused topics.
-