设置存储桶事件通知

最近更新时间: 2024-12-19 17:12:00

功能描述

CSP 支持用户为 Bucket 配置事件通知,能够对用户所关心的对象操作及时进行消息通知,事件通知配置包含一个或多个 topic 通知规则。 目前对象操作包括上传对象类和删除对象类,消息通知端点为kafka。

细节分析

PUT Bucket notification 用于为 Bucket 创建一个新的事件通知配置。如果该 Bucket 已配置事件通知,使用该接口创建新的配置的同时则会覆盖原有的配置。

请求

请求示例

PUT /?notification HTTP/1.1
Host: <BucketName-APPID>.<Endpoint>
Authorization: Auth String 

请求头

此接口仅使用公共请求头部,详情请参见 公共请求头部 文档。

请求体

用户在请求体中使用 XML 语言设置存储桶事件通知配置。

以下为请求体:

<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <TopicConfiguration>
        <Id>123</Id>
        <Topic>topic1</Topic>
        <Endpoint>kafka://user1:passwd@100.99.173.49:9092</Endpoint>
        <KafkaID>123#456</KafkaID>
        <Event>cos:ObjectRemove:Delete</Event>
        <Event>cos:ObjectRemove:DeleteMarkerCreated</Event>
    </TopicConfiguration>
</NotificationConfiguration>

具体元素如下:

节点名称(关键字)父节点描述类型必选
NotificationConfiguration保存TopicConfiguration的实体清单列表Container
TopicConfigurationNotificationConfiguration通知规则描述,缺省时为清理存储桶事件通知Container否(最大50条)
IdTopicConfiguration通知名称String
TopicTopicConfiguration通知的topic名称String
EndpointTopicConfiguration通知端点信息,格式:kafka://[\:\@]\[:\]String
KafkaIDTopicConfigurationkafka实例唯一IDString
EventTopicConfiguration事件列表:缺省时为所有事件String
FilterTopicConfiguration过滤规则Container
3KeyFilter对象名过滤器Container
FilterRule3Key包含Name和Value实体Container
NameFilterRule过滤方法,取值只能为prefix或者suffixstring
ValueFilterRule过滤关键字string

支持的事件类型列表如下:

事件类型描述
cos:ObjectCreated: *上传文件
cos:ObjectCreated:PutPUTObject
cos:ObjectCreated:CopyPUTObjectCopy
cos:ObjectCreated:PostPOSTObject
cos:ObjectCreated:CompleteMultipartUploadCompleteMultipartUpload
cos:ObjectRemove: *删除文件
cos:ObjectRemove:Delete在未开启版本控制的存储桶下,使用 DELETE Object 接口删除对象,或者使用 versionid 删除指定版本的对象
cos:ObjectRemove:DeleteMarkerCreated在开启或者暂停版本控制的存储桶下,使用 DELETE Object 接口删除对象

响应

响应头

此接口仅返回公共响应头部,详情请参见 公共响应头部 文档。

响应体

该请求的响应体返回为空。

错误码

以下描述此请求可能会发生的一些特殊的且常见的错误情况。具体的错误原因可参考返回的 message 进行排查。获取更多关于 CSP 的错误码的信息,或者产品所有的错误列表,请参见 错误码 文档。

错误码HTTP 状态码描述
NoSuchBucket404 Not Found当访问的 Bucket 不存在
MalformedXML400 Bad RequestXML 格式不合法,请跟 restful api 文档仔细比对
InvalidArgument400 Bad RequestId非法,Event非法,或者TopicConfiguration超过50条
NotificationIDConflict400 Bad RequestID冲突
NotificationPrefixAndEventConflict400 Bad Request前缀以及事件类型冲突
InvalidNotificationEndpoint400 Bad RequesEndpoint格式错误

实际案例

请求

以下示例表示给存储桶 examplebucket-1250000000 设置存储桶事件通知。

PUT /?notification HTTP 1.1
Host: <BucketName-APPID>.<Endpoint>
Date: Mon, 17 Jun 2019 08:37:35 GMT
Authorization: signatureValue

<NotificationConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <TopicConfiguration>
        <Id>123</Id>
        <Topic>topic1</Topic>
        <Endpoint>kafka://user1:passwd@100.99.173.49:9092</Endpoint>
        <KafkaID>123#456</KafkaID>
        <Event>cos:ObjectRemove:*</Event>
        <Filter>
            <S3Key>
                <FilterRule>
                    <Name>prefix</Name>
                    <Value>pic</Value>
                </FilterRule>
            </S3Key>
        </Filter>
    </TopicConfiguration>
</NotificationConfiguration>

响应

HTTP/1.1 200 OK
Content-Type: application/xml
Content-Length: 0
Date: Mon, 17 Jun 2019 08:37:36 GMT
Server: tencent-cos
x-cos-request-id: NWQwNzUxNTBfMzdiMDJhMDlfOWM0Nl85NDFk****