事件通知
更新时间:2026-09-21
put_notification
本接口用于指定 bucket 上增加通知规则。
注意:
- 只有 bucket owner 或者 full control 权限才能获取这个 bucket 的配置。
- 如果不是 bucket owner 则返回 403,如果对应的文件不存在则返回 404。
相关参数的详细解释请参见 PutNotification 接口。
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bos.bos_client import BosClient
4
5access_key_id = "your-access-key-id"
6secret_access_key = "your-secret-access-key"
7endpoint = "bj.bcebos.com"
8bucket_name = "your-bucket-name"
9
10config = BceClientConfiguration(
11 credentials=BceCredentials(access_key_id, secret_access_key),
12 endpoint=endpoint
13)
14bos_client = BosClient(config)
15
16notifications = list()
17notifications.append(
18 {
19 "resources": [
20 "/"
21 ],
22 "encryption": {
23 "key": "0123456789abcdef0123456789abcdef"
24 },
25 "status": "enabled",
26 "name": "example-rule",
27 "id": "example-rule-id",
28 "appId": "example-app",
29 "events": [
30 "AppendObject",
31 "CompleteMultipartUpload",
32 "CopyObject",
33 "PutObject",
34 "PostObject",
35 "FetchObject",
36 "DeleteObject"
37 ],
38 "apps": [
39 {
40 "eventUrl": "http://www.example.com/event",
41 "id": "example-app",
42 "xVars": "{\"saveUrl\": \"http://www.example.com/ocr\"}"
43 }
44 ]
45 }
46)
47response = bos_client.put_notification(bucket_name, notifications)
参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
bucket_name |
str / bytes |
是 | 需要增加通知规则的 bucket 名称。 |
notifications |
list[dict] |
是 | 通知规则列表。SDK 会将该参数封装为 {"notifications": notifications} 后提交。 |
config |
BceClientConfiguration |
否 | 可选请求配置。不传时使用客户端初始化配置。 |
notifications 中常用字段说明:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
resources |
list[str] |
是 | 通知规则生效的资源路径,例如 "/" 表示 bucket 下的资源。 |
encryption |
dict |
否 | 通知规则的加密配置,可包含 key 字段。 |
status |
str |
是 | 通知规则状态,例如 enabled。 |
name |
str |
是 | 通知规则名称。 |
id |
str |
是 | 通知规则 ID。 |
appId |
str |
是 | 接收通知的应用 ID。 |
events |
list[str] |
是 | 触发通知的事件列表,例如 PutObject、DeleteObject 等。 |
apps |
list[dict] |
是 | 应用配置列表,可包含 eventUrl、id、xVars 等字段。 |
返回说明:
调用成功后返回 BceResponse 对象。可通过 response.status_code 查看 HTTP 状态码,通过 response.metadata 查看响应头信息。实际请求成功时,服务端返回 200 OK,响应体为空。
get_notification
本接口用于获取指定 bucket 上的通知规则。
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bos.bos_client import BosClient
4
5access_key_id = "your-access-key-id"
6secret_access_key = "your-secret-access-key"
7endpoint = "bj.bcebos.com"
8bucket_name = "your-bucket-name"
9
10config = BceClientConfiguration(
11 credentials=BceCredentials(access_key_id, secret_access_key),
12 endpoint=endpoint
13)
14bos_client = BosClient(config)
15
16response = bos_client.get_notification(bucket_name)
参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
bucket_name |
str / bytes |
是 | 需要获取通知规则的 bucket 名称。 |
config |
BceClientConfiguration |
否 | 可选请求配置。不传时使用客户端初始化配置。 |
返回说明:
调用成功后返回 BceResponse 对象。可通过 response.status_code 查看 HTTP 状态码,通过 response.get_body_map() 获取响应体内容。响应体中包含 notifications 列表,每个元素为一条通知规则配置。
如果指定 bucket 上不存在通知规则,服务端返回 404 NoNotificationConfiguration。
delete_notification
本接口用于删除指定 bucket 上的通知规则。
注意:
该接口为一次性全部删除当前 bucket 下全部规则。
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bos.bos_client import BosClient
4
5access_key_id = "your-access-key-id"
6secret_access_key = "your-secret-access-key"
7endpoint = "bj.bcebos.com"
8bucket_name = "your-bucket-name"
9
10config = BceClientConfiguration(
11 credentials=BceCredentials(access_key_id, secret_access_key),
12 endpoint=endpoint
13)
14bos_client = BosClient(config)
15
16response = bos_client.delete_notification(bucket_name)
参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
bucket_name |
str / bytes |
是 | 需要删除通知规则的 bucket 名称。 |
config |
BceClientConfiguration |
否 | 可选请求配置。不传时使用客户端初始化配置。 |
返回说明:
调用成功后返回 BceResponse 对象。可通过 response.status_code 查看 HTTP 状态码,通过 response.metadata 查看响应头信息。实际删除成功时,服务端返回 204 No Content,响应体为空。
评价此篇文章
