Configuring a DIS Notification Policy
Function
You can call this API to configure a DIS notification policy to push object change events in an OBS bucket to a DIS stream. The API is idempotent. If there is already an identical policy for the bucket, a success message is returned with status code 200. Otherwise, status code 201 is returned.
DIS notification policies are not available for fusion buckets.
Restrictions
- To configure, obtain, or delete DIS notification policies, you must have the Tenant Administrator permission assigned by using role/policy-based authorization of IAM. To use DIS notifications, an IAM agency that assigns OBS the DIS User permission by using role/policy-based authorization is also required.
- A maximum of 10 DIS notification rules can be configured for a bucket.
- For services that require responses within seconds, the notifications cannot guarantee the required latency.
- New DIS notification rules are applied within 5 minutes.
- DIS notification rules configured for the same bucket cannot overlap with each other. If the notification rule you are creating overlaps with an existing one, the rule creation will fail.
For an event, if a rule has been configured without specifying the object name prefix or suffix, no more rules can be configured for the same event. Because the rule without specifying the object name prefix or suffix applies to all objects in the bucket.
For example, suppose there is already a rule A for PUT events, with the prefix set to abcd and the suffix set to .txt. If you need to create another rule B for the same PUT events, configure its prefix and suffix by referring to Table 1 to make sure that your creation is successful.
Table 1 Prefix and suffix settings and creation results of rule B Prefix and Suffix of Rule A
Prefix and Suffix of Rule B
Rule B Creation Result
Reason
Prefix: abcd
Suffix: .txt
Prefix: abcd
Suffix: .txt
Failed
Both rules have the same prefix and suffix.
Prefix: abcd
Suffix: left blank
Failed
Both rules have the same prefix, and the suffix of rule A is included in that of rule B.
Prefix: ab
Suffix: xt
Failed
The prefix and suffix of rule A are included in those of rule B.
Prefix: abef
Suffix: .txt
Successful
Both rules have the same suffix, but different prefixes.
Prefix: abcd
Suffix: .mp4
Successful
Both rules have the same prefix, but different suffixes.
Authorization Information
To call this API, you must have the Tenant Administrator permission. To configure the Tenant Administrator permission, you need to use role/policy-based authorization (IAM v3 APIs in the old IAM version). For details, see Using IAM for Authorization.
Method
func (obsClient ObsClient) SetDisPolicy(input *SetDisPolicyInput, extensions ...extensionOptions) (output *BaseModel, err error)
Request Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| bucketName | string | Yes | Explanation: Bucket name. |
Response Parameter Description
| Field | Type | Description |
|---|---|---|
| BaseModel | BaseModel | Explanation: Basic model, including RequestId. |
| Rules | []DisPolicyRule | DIS policy rule list. Restrictions: For the same bucket, prefixes of different rules cannot be the same or contain each other. The same agency is recommended. Value range: The size of the array is from 1 to 10. |
| Field | Type | Description |
|---|---|---|
| ID | string | Rule ID. Value range: [1, 256]. The value must comply with ^[a-zA-Z0-9_-]{1, 256}$. |
| Stream | string | DIS stream name. You need to create a DIS stream first. |
| Project | string | ID of the project to which the DIS stream belongs. |
| Events | []string | OBS event list. A valid value must contain 0 to 1,023 characters. Any characters are allowed. The following event types are supported:
|
| Prefix | string | Object name prefix, key word of object names. Based on the prefix defined by this parameter, enter the key word for filtering objects. A longer string of characters delivers a more accurate filtering result. A maximum of 1,024 characters are supported. It also can be empty. In addition, the total length of the prefix and suffix cannot exceed 1024 characters. |
| Suffix | string | Object name suffix. Key word of object names. Based on the suffix defined by this parameter, enter the key word for filtering objects. A longer string of characters delivers a more accurate filtering result. A maximum of 1,024 characters are supported. It also can be empty. In addition, the total length of the prefix and suffix cannot exceed 1024 characters. |
| Agency | string | IAM agency name. The delegated party must include OBS and be granted the DIS Administrator or DIS User permission. |
Sample Code
Example 1: This example creates a DIS notification policy for bucket examplebucket to notify the DIS stream my-dis-stream (which can be purchased and viewed on the DIS console) of the upload and deletion events of all objects.
package main
import (
"fmt"
"log"
obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)
func main() {
// Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage.
// 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.
ak := os.Getenv("AccessKeyID")
sk := os.Getenv("SecretAccessKey")
// (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are not advised to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways.
securityToken := os.Getenv("SecurityToken")
// Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
endPoint := "https://obs.ap-southeast-1.myhuaweicloud.com"
// Create a client.
obsClient, err := obs.New(ak, sk, endPoint, obs.WithSecurityToken(securityToken))
if err != nil {
fmt.Printf("Create obsClient error, errMsg: %s", err.Error())
}
// Configure a DIS policy.
input := &obs.SetDisPolicyInput{
Bucket: "examplebucket",
Rules: []obs.DisPolicyRule{
{
ID: "dis-rule-01",
Stream: "my-dis-stream",
Project: "project-id-12345",
Events: []string{
"ObjectCreated:*",
"ObjectRemoved:*",
},
Agency: "obs_dis_agency",
},
},
}
output, err := client.SetDisPolicy(input)
if err != nil {
log.Fatalf("Failed to set DIS policy: %v", err)
}
fmt.Printf("Request ID: %s\n", output.RequestId)
fmt.Println("DIS policy set successfully")
} Example 2: This example creates a DIS notification policy with multiple rules for bucket examplebucket to monitor the creation of objects whose name prefix is upload/ and the deletion of objects whose name suffix is .log. The events are pushed to the DIS stream my-dis-stream (which can be purchased and viewed on the DIS console).
package main
import (
"fmt"
"log"
obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)
func main() {
// Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage.
// 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.
ak := os.Getenv("AccessKeyID")
sk := os.Getenv("SecretAccessKey")
// (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are not advised to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways.
securityToken := os.Getenv("SecurityToken")
// Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
endPoint := "https://obs.ap-southeast-1.myhuaweicloud.com"
// Create a client.
obsClient, err := obs.New(ak, sk, endPoint, obs.WithSecurityToken(securityToken))
if err != nil {
fmt.Printf("Create obsClient error, errMsg: %s", err.Error())
}
// Configure a DIS policy with multiple rules.
input := &obs.SetDisPolicyInput{
Bucket: "examplebucket",
Rules: []obs.DisPolicyRule{
{
ID: "create-rule",
Stream: "my-dis-stream",
Project: "project-id-12345",
Events: []string{
"ObjectCreated:*",
},
Prefix: "upload/",
Agency: "obs_dis_agency",
},
{
ID: "delete-rule",
Stream: "my-dis-stream",
Project: "project-id-12345",
Events: []string{
"ObjectRemoved:*",
},
Suffix: ".log",
Agency: "obs_dis_agency",
},
},
}
output, err := client.SetDisPolicy(input)
if err != nil {
log.Fatalf("Failed to set multi-rule DIS policy: %v", err)
}
fmt.Printf("Request ID: %s\n", output.RequestId)
fmt.Printf("Configured %d rules\n", len(input.Rules))
fmt.Println("Multi-rule DIS policy set successfully")
} Example 3: This example deletes the DIS notification rule whose ID is rule-001 from bucket examplebucket.
package main
import (
"fmt"
"log"
obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)
func main() {
// Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage.
// 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.
ak := os.Getenv("AccessKeyID")
sk := os.Getenv("SecretAccessKey")
// (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are not advised to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways.
securityToken := os.Getenv("SecurityToken")
// Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
endPoint := "https://obs.ap-southeast-1.myhuaweicloud.com"
// Create a client.
obsClient, err := obs.New(ak, sk, endPoint, obs.WithSecurityToken(securityToken))
if err != nil {
fmt.Printf("Create obsClient error, errMsg: %s", err.Error())
}
bucketName := "examplebucket"
idsToDelete := []string{"rule-001"}
// Step 1: Obtain the current policy.
currentPolicy, err := client.GetDisPolicy(bucketName)
if err != nil {
log.Fatalf("Failed to obtain the DIS policy: %v", err)
}
fmt.Printf("Current number of rules: %d\n", len(currentPolicy.Rules))
// Step 2: Construct the ID mapping for deletion.
idMap := make(map[string]bool)
for _, id := range idsToDelete {
idMap[id] = true
}
// Step 3: Delete the target rule from the rule list.
var updatedRules []obs.DisPolicyRule
for _, rule := range currentPolicy.Rules {
if !idMap[rule.ID] {
updatedRules = append(updatedRules, rule)
}
}
// Step 4: Reconfigure the policy.
setInput := &obs.SetDisPolicyInput{
Bucket: bucketName,
Rules: updatedRules,
}
_, err = client.SetDisPolicy(setInput)
if err != nil {
log.Fatalf("Failed to set the new policy: %v", err)
}
fmt.Printf("Successfully deleted %d rules. Number of remaining rules: %d\n", len(idsToDelete), len(updatedRules))
} Example 4: This example adds a DIS rule to bucket examplebucket, where a DIS policy has already been configured.
package main
import (
"fmt"
"log"
obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)
func main() {
// Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage.
// 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.
ak := os.Getenv("AccessKeyID")
sk := os.Getenv("SecretAccessKey")
// (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are not advised to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways.
securityToken := os.Getenv("SecurityToken")
// Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
endPoint := "https://obs.ap-southeast-1.myhuaweicloud.com"
// Create a client.
obsClient, err := obs.New(ak, sk, endPoint, obs.WithSecurityToken(securityToken))
if err != nil {
fmt.Printf("Create obsClient error, errMsg: %s", err.Error())
}
// Obtain the existing policy.
output, err := client.GetDisPolicy("examplebucket")
if err != nil {
log.Fatalf("Failed to get DIS policy: %v", err)
}
// Add a new rule.
newRules := append(output.Rules, obs.DisPolicyRule{
ID: "new-rule",
Stream: "my-dis-stream",
Project: "project-id-12345",
Events: []string{
"ObjectCreated:*",
},
Agency: "obs_dis_agency",
})
// Update the policy.
updateInput := &obs.SetDisPolicyInput{
Bucket: "examplebucket",
Rules: newRules,
}
_, err = client.SetDisPolicy(updateInput)
if err != nil {
log.Fatalf("Failed to update DIS policy: %v", err)
}
} Helpful Links
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