设置事件通知(C SDK)
您可以通过函数set_notification_configuration设置桶的事件通知。
方法定义
void set_notification_configuration(const obs_options *options,
obs_smn_notification_configuration* notification_conf, obs_response_handler *handler,
void *callback_data); 参数描述
| 参数名称 | 参数类型 | 是否必选 | 描述 |
|---|---|---|---|
| options | 请求桶的上下文,配置option(C SDK) | 必选 | 桶参数 |
| notification_conf | obs_smn_notification_configuration* | 必选 | 消息通知配置事件通知的容器 |
| handler | obs_response_handler * | 必选 | 回调函数 |
| callback_data | void * | 可选 | 回调数据 |
参数obs_smn_notification_configuration描述如下表:
| 参数名称 | 参数类型 | 是否必选 描述 |
|---|---|---|
| topic_conf | obs_smn_topic_configuration* | 配置事件通知主题的容器 |
| topic_conf_num | unsigned int | topic_conf的个数 |
参数obs_smn_topic_configuration描述如下表:
| 参数名称 | 参数类型 | 是否必选 描述 |
|---|---|---|
| topic | char * | 事件通知主题的URN,当OBS检测到桶中发生特定的事件后,将会发布通知消息至该主题 |
| id | char * | 每项事件通知配置的唯一标识,如果用户未指定ID,系统将自动分配一个ID |
| filter_rule | obs_smn_filter_rule * | 过滤规则键值对的容器 |
| filter_rule_num | unsigned int | filter_rule的个数 |
| event | obs_smn_event_enum* | 需要发布通知消息的事件类型 |
| event_num | unsigned int | 事件类型的个数 |
代码示例
static void test_set_notification_configuration()
{
obs_options option;
obs_status ret_status = OBS_STATUS_BUTT;
obs_smn_notification_configuration notification_conf;
obs_smn_topic_configuration topic_conf;
obs_smn_event_enum topic1_event[2];
obs_smn_filter_rule filter_rule;
// 设置option
init_obs_options(&option);
option->bucket_options.hostName = "<your-endpoint>";
option->bucket_options.bucketName = "<Your bucketname>";
// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY。
// 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
option.bucket_options.access_key = getenv("ACCESS_KEY_ID");
option.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
// 设置回调函数
obs_response_handler response_handler =
{
NULL, &response_complete_callback
};
// 设置通知配置的id,该配置唯一标识
topic_conf.id = "Id001";
// 设置事件通知主题的URN
topic_conf.topic = "urn:smn:southchina:ea79855fbe0642718cb4df1551c3cb4e:test_cwx298983";
// 设置通知的操作
topic_conf.event = topic1_event;
topic_conf.event[0] = SMN_EVENT_OBJECT_CREATED_ALL;
topic_conf.event[1] = SMN_EVENT_OBJECT_CREATED_POST;
topic_conf.event_num = 2;
// 设置通知对象的过滤规则
filter_rule.name = OBS_SMN_FILTER_PREFIX;
filter_rule.value = "aaa";
topic_conf.filter_rule = &filter_rule;
topic_conf.filter_rule_num = 1;
memset(¬ification_conf, 0, sizeof(obs_smn_notification_configuration));
notification_conf.topic_conf = &topic_conf;
notification_conf.topic_conf_num = 1;
set_notification_configuration(&option, ¬ification_conf, &response_handler, &ret_status);
if (OBS_STATUS_OK == ret_status) {
printf("set_notification_configuration success.\n");
}
else {
printf("set_notification_configuration failed(%s).\n",
obs_get_status_name(ret_status));
}
}