Setting Event Notifications (SDK for C)
You can call set_notification_configuration to set event notifications for a bucket.
Method
void set_notification_configuration(const obs_options *options,
obs_smn_notification_configuration* notification_conf, obs_response_handler *handler,
void *callback_data); Parameter Description
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| options | The context of the bucket request. For details, see Configuring option (SDK for C). | Mandatory | Bucket parameter |
| notification_conf | obs_smn_notification_configuration* | Mandatory | Indicates the container for configuring the event notification. |
| handler | obs_response_handler * | Mandatory | Callback function |
| callback_data | void * | Optional | Callback data |
The following table describes the parameter obs_smn_notification_configuration.
| Parameter | Type | Mandatory (Yes/No) Description |
|---|---|---|
| topic_conf | obs_smn_topic_configuration* | Container for configuring the event notification topic |
| topic_conf_num | unsigned int | Number of topic_conf |
The following table describes the parameter obs_smn_topic_configuration.
| Parameter | Type | Mandatory (Yes/No) Description |
|---|---|---|
| topic | char * | URN of the event notification topic. After detecting a specific event in the bucket, OBS sends a message to the topic. |
| id | char * | Unique ID of each event notification. If no ID is specified, the system automatically assigns one. |
| filter_rule | obs_smn_filter_rule * | Container that defines key-value pairs of filter rules |
| filter_rule_num | unsigned int | Number of filter_rule |
| event | obs_smn_event_enum* | Type of events that need to be notified |
| event_num | unsigned int | Number of event types |
Code Examples
#include "eSDKOBS.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// The response callback function. In this callback, you can record the content of properties into callback_data (custom callback data).
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data);
// The completion callback function. In this callback, you can record the content of obs_status and obs_error_details into callback_data (custom callback data).
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data);
int main()
{
// The following example configures event notifications for a bucket:
// Call the obs_initialize method at the program entry to initialize global resources such as the network and memory.
obs_status ret_status = obs_initialize(OBS_INIT_ALL);
if (OBS_STATUS_OK != ret_status)
{
printf("obs_initialize failed(%s).\n", obs_get_status_name(ret_status));
return -1;
}
obs_options options;
// Create and initialize options, including the access domain name (host_name), access keys (access_key_id and access_key_secret), bucket name (bucket_name), and bucket storage class (storage_class).
init_obs_options(&options);
// Enter the endpoint corresponding to the bucket for host_name. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
options.bucket_options.host_name = "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.
// Obtain an AK/SK pair on the management console.
options.bucket_options.access_key = getenv("ACCESS_KEY_ID");
options.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
// Specify the bucket name, for example, example-bucket-name.
char * bucketName = "example-bucket-name";
options.bucket_options.bucket_name = bucketName;
// Set the callback function.
obs_response_handler response_handler = { &response_properties_callback, &response_complete_callback };
// Configure event notifications: When an object with a specified prefix in its name is created, a message is sent to the SMN topic.
obs_smn_event_enum topic1_event[2];
topic1_event[0] = SMN_EVENT_OBJECT_CREATED_ALL;
// Configure the filter rule for objects that trigger notifications.
obs_smn_filter_rule filter_rule;
memset(&filter_rule, 0, sizeof(obs_smn_filter_rule));
filter_rule.name = OBS_SMN_FILTER_PREFIX;
filter_rule.value = "aaa";
// Set the unique ID of the notification configuration.
obs_smn_topic_configuration topic_conf;
memset(&topic_conf, 0, sizeof(obs_smn_topic_configuration));
topic_conf.id = "Id001";
// Set the URN of the event notification topic.
topic_conf.topic = "urn:smn:southchina:ea79855fbe0642718cb4df1551c3cb4e:test_cwx298983";
// Set the event types that trigger notifications.
topic_conf.event = topic1_event;
topic_conf.event_num = 1;
// Configure the filter rule for objects that trigger notifications.
topic_conf.filter_rule = &filter_rule;
topic_conf.filter_rule_num = 1;
// Configure the notifications.
obs_smn_notification_configuration notification_conf;
memset(¬ification_conf, 0, sizeof(obs_smn_notification_configuration));
notification_conf.topic_conf = &topic_conf;
notification_conf.topic_conf_num = 1;
ret_status = OBS_STATUS_BUTT;
set_notification_configuration(&options, ¬ification_conf,
&response_handler, &ret_status);
if (OBS_STATUS_OK == ret_status) {
printf("set_notification_configuration successfully.\n");
}
else {
printf("set_notification_configuration failed(%s).\n",
obs_get_status_name(ret_status));
}
// Release the allocated global resources.
obs_deinitialize();
return 0;
}
// The response callback function. In this callback, you can record the content of properties into callback_data (custom callback data).
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data)
{
if (properties == NULL)
{
printf("error! obs_response_properties is null!");
return OBS_STATUS_OK;
}
// Print the response.
#define print_nonnull(name, field) \
do { \
if (properties-> field) { \
printf("%s: %s\n", name, properties->field); \
} \
} while (0)
print_nonnull("request_id", request_id);
print_nonnull("request_id2", request_id2);
print_nonnull("content_type", content_type);
if (properties->content_length) {
printf("content_length: %lu\n", properties->content_length);
}
print_nonnull("server", server);
print_nonnull("ETag", etag);
print_nonnull("expiration", expiration);
print_nonnull("website_redirect_location", website_redirect_location);
print_nonnull("version_id", version_id);
print_nonnull("allow_origin", allow_origin);
print_nonnull("allow_headers", allow_headers);
print_nonnull("max_age", max_age);
print_nonnull("allow_methods", allow_methods);
print_nonnull("expose_headers", expose_headers);
print_nonnull("storage_class", storage_class);
print_nonnull("server_side_encryption", server_side_encryption);
print_nonnull("kms_key_id", kms_key_id);
print_nonnull("customer_algorithm", customer_algorithm);
print_nonnull("customer_key_md5", customer_key_md5);
print_nonnull("bucket_location", bucket_location);
print_nonnull("obs_version", obs_version);
print_nonnull("restore", restore);
print_nonnull("obs_object_type", obs_object_type);
print_nonnull("obs_next_append_position", obs_next_append_position);
print_nonnull("obs_head_epid", obs_head_epid);
print_nonnull("reserved_indicator", reserved_indicator);
int i;
for (i = 0; i < properties->meta_data_count; i++) {
printf("x-obs-meta-%s: %s\n", properties->meta_data[i].name,
properties->meta_data[i].value);
}
return OBS_STATUS_OK;
}
// The completion callback function. In this callback, you can record the content of obs_status and obs_error_details into callback_data (custom callback data).
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data)
{
if (callback_data) {
obs_status *ret_status = (obs_status *)callback_data;
*ret_status = status;
}
else {
printf("Callback_data is NULL");
}
if (error && error->message) {
printf("Error Message: \n %s\n", error->message);
}
if (error && error->resource) {
printf("Error Resource: \n %s\n", error->resource);
}
if (error && error->further_details) {
printf("Error further_details: \n %s\n", error->further_details);
}
if (error && error->extra_details_count) {
int i;
for (i = 0; i < error->extra_details_count; i++) {
printf("Error Extra Detail(%d):\n %s:%s\n", i, error->extra_details[i].name,
error->extra_details[i].value);
}
}
if (error && error->error_headers_count) {
int i;
for (i = 0; i < error->error_headers_count; i++) {
const char *errorHeader = error->error_headers[i];
printf("Error Headers(%d):\n %s\n", i, errorHeader == NULL ? "NULL Header" : errorHeader);
}
}
} 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