Configuring Event Notifications (SDK for Java)
Function
You can configure notifications for certain types of events. When such events happen in your bucket, you will be notified of the operations in a timely manner.
This API is used to configure event notifications for a bucket.
Restrictions
- To configure event notifications for a bucket, you must be the bucket owner or have the required permission (obs:bucket:PutBucketNotification granted using IAM or PutBucketNotification granted using a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Creating a Custom Bucket Policy.
- The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
- Before configuring event notifications, you need to create a topic on the SMN console and configure a topic policy to grant message publishing permissions to OBS.
Method
ObsClient.setBucketNotification(final SetBucketNotificationRequest request)
Request Parameters
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| request | Yes | Explanation: Request parameters. For details, see Table 2. |
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| bucketName | String | Yes | Explanation: Bucket name. Restrictions:
Default value: None |
| bucketNotificationConfiguration | Yes | Explanation: Array of rules. For details, see Table 3. |
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| topicConfigurations | List<Table 4> | No | Explanation: List of event notification topic configurations. Value range: For details, see Table 4. Default value: None |
| functionGraphConfigurations | List<Table 5> | No | Explanation: List of FunctionGraph configurations. Value range: For details, see Table 5. Default value: None |
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| id | String | No | Explanation: Unique ID of each event notification configuration. If the ID is not specified, OBS automatically assigns an ID. Restrictions: None Default value: None |
| filter | Yes | Explanation: It stores rules for filtering object names. Value range: For details, see Table 6. Default value: None | |
| topic | String | Yes | Explanation: URN of the event notification topic. When OBS detects a specific event in the bucket, it publishes a notification message to the topic. You can find the topic's URN on the Topics page of the SMN console. Restrictions: None Default value: None |
| events | List<Table 7> | No | Explanation: List of event types for which notifications will be sent. Value range: For details, see Table 7. Default value: None |
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| id | String | No | Explanation: Unique ID of each event notification configuration. If the ID is not specified, OBS automatically assigns an ID. Restrictions: None Default value: None |
| filter | Yes | Explanation: It stores rules for filtering object names. Value range: For details, see Table 6. Default value: None | |
| functionGraph | String | Yes | Explanation: URN of FunctionGraph. When OBS detects a specific event in the bucket, it sends a message to FunctionGraph and calls FunctionGraph. Restrictions: None Default value: None |
| events | List<Table 7> | No | Explanation: List of event types for which notifications will be sent. Value range: For details, see Table 7. Default value: None |
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| filterRules | List<Table 8> | Yes | Explanation: It stores rules for filtering object names. Value range: For details, see Table 8. Default value: None |
| Constant | Description |
|---|---|
| OBJECT_CREATED_ALL | All events for creating objects, including uploading objects using PUT and POST, copying objects, and completing multipart uploads. |
| OBJECT_CREATED_PUT | All events for uploading objects using PUT. |
| OBJECT_CREATED_POST | All events for uploading objects using POST. |
| OBJECT_CREATED_COPY | All events for copying objects. |
| OBJECT_CREATED_COMPLETE_MULTIPART_UPLOAD | All events for completing multipart uploads. |
| OBJECT_REMOVED_ALL | Events of calling the APIs for deleting objects or aborting multipart uploads. These events include deleting objects by specifying object version IDs, and deleting objects without specifying object version IDs after versioning is enabled. |
| OBJECT_REMOVED_DELETE | Events for deleting objects by specifying object version IDs. |
| OBJECT_REMOVED_DELETE_MARKER_CREATED | Events for deleting objects without specifying object version IDs after versioning is enabled. |
Responses
| Parameter | Type | Description |
|---|---|---|
| statusCode | int | Explanation: HTTP status code. Value range: A status code is a group of digits that can be 2xx (indicating successes) or 4xx or 5xx (indicating errors). It indicates the status of a response. For more information, see Status Code. Default value: None |
| responseHeaders | Map<String, Object> | Explanation: HTTP response header list, composed of tuples. In a tuple, the String key indicates the name of the header, and the Object value indicates the value of the header. Default value: None |
Sample Code
Sample code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use. String endPoint = "https://obs.ap-southeast-1.myhuaweicloud.com"; // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); // Create an ObsClient instance. ObsClient obsClient = new ObsClient(ak, sk, endPoint); BucketNotificationConfiguration bucketNotificationConfig = new BucketNotificationConfiguration(); TopicConfiguration topicConfig = new TopicConfiguration(); //Customize a message notification ID. topicConfig.setId("id1"); //Topic URN. You need to create a topic, for example, urn:smn:region:project_id:smn_topic, on SMN. topicConfig.setTopic("your topic"); topicConfig.getEventTypes().add(EventTypeEnum.OBJECT_CREATED_ALL); Filter topicFilter = new Filter(); topicFilter.getFilterRules().add(new FilterRule("prefix", "smn")); topicFilter.getFilterRules().add(new FilterRule("suffix", ".jpg")); topicConfig.setFilter(topicFilter); bucketNotificationConfig.addTopicConfiguration(topicConfig); obsClient.setBucketNotification("bucketname", bucketNotificationConfig); |
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot