All Products
Search
Document Center

Object Storage Service:Use MNS to send notifications of OSS events

Last Updated:Jan 23, 2024

You can configure event notification rules in the Object Storage Service (OSS) console for objects that you want to monitor. If the events that are specified in the rules occur on these objects, a notification is sent from the HTTP servers or Message Service (MNS) queues that are specified in the rules.

Example scenario

You create a bucket named srcbucket in the China (Hangzhou) region for your company, and create two directories log/ and destdir/ in srcbucket. The log/ directory is used to store log objects such as log/date1.txt, log/date2.txt, and log/date3.txt. The destdir/ directory is used to store image objects used in customer case analysis, such as destdir/photo1.jpg and destdir/photo2.jpg. The following structure shows the directories and objects in srcbucket:

srcbucket
    └── log/
       ├── date1.txt
       ├── date2.txt
       ├── date3.txt
       ├── .......
    └── destdir/       
       ├── photo1.jpg
       ├── photo2.jpg
       ├── ...... 

The company needs to synchronize logs and customer case images in srcbucket and all changes to these objects (such as addition, deletion, and modification of objects) to a bucket named destbucket in the UK (London) region. In addition, the company wants all its employees to be notified of changes to objects in the log/ and destdir/ directories in srcbucket and destbucket.

In this case, you can configure a cross-region replication (CRR) rule for srcbucket and event notification rules for srcbucket and destbucket.

Step 1: Create MNS queues

  1. Log on to the MNS console.

  2. In the left-side navigation pane, click Queues.

  3. In the top navigation bar, select the China (Hangzhou) region.

  4. On the Queues page, click Create Queue.

  5. In the Create Queue panel, set the queue name to myqueue1 and retain the default settings for other parameters.

  6. Click OK.

  7. Switch to the UK (London) region in the top navigation bar and repeat the preceding steps to create a queue named myqueue2 for receiving notifications of events on destbucket.

Step 2: Configure a CRR rule for srcbucket

  1. Log on to the OSS console.

  2. In the left-side navigation pane, click Buckets. On the page that appears, click srcbucket.

  3. In the left-side navigation tree, choose Data Management > CRR.

  4. On the CRR tab, click CRR.

  5. In the CRR panel, configure the following parameters.

    Parameter

    Description

    Destination Bucket

    Select UK (London) and then select destbucket.

    Objects to Replicate

    Select Objects with Specified Prefix, and specify prefixes destdir/ and log/.

    Replication Policy

    Select Add/Delete/Change.

    Replicate Historical Data

    Select Yes.

    RAM Role Name

    Select a role for CRR.

  6. Select the check box to confirm that you have read and agree to the compliance commitment for cross-border data transfer, and then click OK.

Step 3: Configure event notification rules for srcbucket and destbucket

Important

The CRR rule configured in Step 2 specifies that historical data is replicated to destbucket. If a large amount of historical data is available, lots of notifications are triggered when the replication happens. If you do not want to be notified during the replication, we recommend that you configure event notification rules for srcbucket and destbucket after all historical data is synchronized.

  1. Configure event notifications for srcbucket.

    1. Log on to the OSS console.

    2. In the left-side navigation pane, click Buckets. On the page that appears, click srcbucket.

    3. In the left-side navigation tree, choose Data Processing > Event Notification.

    4. On the Event Notification page, click Create Rule.

    5. In the Create Rule panel, configure the parameters according to the following table and click OK.

      Parameter

      Description

      Rule Name

      Set the rule name to notification1.

      Event Type

      Select PutObject, CopyObject, DeleteObject, and DeleteObjects.

      Resource Description

      Select Prefix and Suffix, and then add prefixes log/ and destdir/.

      Endpoint

      Select Queue, and then enter myqueue1 that you created in Step 1.

  2. Configure event notifications for destbucket.

    1. In the left-side navigation pane, click Buckets. On the page that appears, click destbucket.

    2. In the left-side navigation tree, choose Data Processing > Event Notification.

    3. On the Event Notification page, click Create Rule.

    4. In the Create Rule panel, configure the parameters according to the following table and click OK.

      Parameter

      Description

      Rule Name

      Set the rule name to notification2.

      Event Type

      Select ObjectReplication:ObjectCreated, ObjectReplication:ObjectRemoved, and ObjectReplication:ObjectModified.

      Resource Description

      Select Prefix and Suffix, and then add prefixes log/ and destdir/.

      Endpoint

      Select Queue, and then enter myqueue2 that you created in Step 1.

It takes about 10 minutes for new event notification rules to take effect.

Step 4: Configure subscriptions to receive notifications

When an event matching rule is triggered, MNS automatically creates a topic. The topic name is in the mns-en-topics-[Product]-[RuleName]-[Timestamp] format, such as, mns-en-topics-oss-notification1-1700098799. To receive notifications, you must subscribe to the topic and specify an endpoint in the subscription rule.

  1. Create subscriptions to topics.

    1. Log on to the MNS console.

    2. In the left-side navigation pane, click Topics.

    3. In the top navigation bar, select the China (Hangzhou) region.

    4. Locate the automatically created topic and click View Subscriptions in the Actions column.

    5. In the panel that appears, click Subscriptions. On the page that appears, click Create Subscription.

    6. In the Create Subscription panel, configure parameters to create a subscription to the topic that is created by MNS for srcbucket. Enter mysubscription1 for Name, select Queue for Push Type, and then enter myqueue1 for Receiver Endpoint. Retain the default settings for other parameters.

    7. Click OK.

    8. Switch to the UK (London) region and repeat the preceding steps to create the mysubscription2 subscription to the topic that is created by MNS for destbucket. Select Queue for the push type and enter myqueue2 for the receiver endpoint. Retain the default settings for other parameters.

  2. Configure settings to receive notifications.

    1. In the left-side navigation pane, click Queues.

    2. Locate myqueue1 and choose More > Send Messages in the Actions column.

    3. In the Receive Message section of the displayed page, click Receive Message.

      The system notifies you when objects in the log/ and destdir/ directories of srcbucket are created, modified, or deleted.

    4. Repeat the preceding steps to receive notifications from myqueue2 that is created for destbucket. After the configuration is complete, you will receive event notifications about object creation, overwriting, or deletion in destbucket based on the CRR rule.

    If you no longer need such notifications, delete the event notification rule. Deleting an event notification rule does not delete the automatically created topic. Therefore, to prevent unnecessary charges, delete unwanted topics at the earliest opportunity.