Updated on 2026-07-31 GMT+08:00

Configuring Event Notifications for a Bucket (SDK for Python)

Function

You can configure notifications for certain types of events. When such events happen in your bucket, you will be notified of the operations in a timely manner.

This API configures event notifications for a bucket.

Restrictions

Method

ObsClient.setBucketNotification(bucketName, notification, extensionHeaders)

Request Parameters

Parameter

Type

Mandatory (Yes/No)

Description

bucketName

str

Yes

Explanation:

Bucket name

Restrictions:

  • A bucket name must be unique across all accounts and regions.
  • A bucket name:
    • Must be 3 to 63 characters long and start with a digit or letter. Lowercase letters, digits, hyphens (-), and periods (.) are allowed.
    • Cannot be formatted as an IP address.
    • Cannot start or end with a hyphen (-) or period (.).
    • Cannot contain two consecutive periods (..), for example, my..bucket.
    • Cannot contain periods (.) and hyphens (-) adjacent to each other, for example, my-.bucket or my.-bucket.
  • If you repeatedly create buckets of the same name in the same region, no error will be reported and the bucket properties comply with those set in the first creation request.

Default value:

None

notification

Notification

No

Explanation:

Event notification configurations If this parameter is left blank, there is no event notification configured. For details, see Table 1.

Default value:

None

extensionHeaders

dict

No

Explanation:

Extension headers.

Value range:

See User-defined Headers (SDK for Python).

Default value:

None

Table 1 TopicConfiguration

Parameter

Type

Mandatory (Yes/No)

Description

id

str

No if used as a request parameter

Event notification setting ID

topic

str

Yes if used as a request parameter

URN of the event notification topic. After detecting a specific event, OBS sends a message to the topic.

events

list of str

Yes if used as a request parameter

Event types for which a notification needs to be sent

filterRules

list of FilterRule

No if used as a request parameter

List of filtering rules

Table 2 EventType

Constant

Description

OBJECT_CREATED_ALL

All events for creating objects, including uploading objects through PUT and POST, copying objects, and combining parts

OBJECT_CREATED_PUT

PUT Object events

OBJECT_CREATED_POST

POST Object events

OBJECT_CREATED_COPY

Object copying events

OBJECT_CREATED_COMPLETE_MULTIPART_UPLOAD

Parts merging events

OBJECT_REMOVED_ALL

Events of calling the APIs for deletion and aborting multipart uploads

Events for deleting objects without specifying object version IDs, when versioning is enabled, as well as deleting objects by specifying object version IDs

OBJECT_REMOVED_DELETE

Events for deleting objects by specifying object version IDs

OBJECT_REMOVED_DELETE_MARKER_CREATED

Events for deleting objects without specifying object version IDs, when versioning is enabled

Table 3 FilterRule

Parameter

Type

Mandatory (Yes/No)

Description

name

str

No if used as a request parameter

Prefix or suffix of object names for filtering. Possible values are:

  • prefix
  • suffix

value

str

No if used as a request parameter

Keyword of object names for filtering objects by prefix or suffix

Responses

Type

Description

GetResult

Explanation:

SDK common results

Table 4 GetResult

Parameter

Type

Description

status

int

Explanation:

HTTP status code

Value range:

A status code is a group of digits indicating the status of a response. It ranges from 2xx (indicating successes) to 4xx or 5xx (indicating errors). For more information, see Status Code.

Default value:

None

reason

str

Explanation:

Reason description

Default value:

None

errorCode

str

Explanation:

Error code returned by the OBS server. If the value of status is less than 300, this parameter is left blank.

Default value:

None

errorMessage

str

Explanation:

Error message returned by the OBS server. If the value of status is less than 300, this parameter is left blank.

Default value:

None

requestId

str

Explanation:

Request ID returned by the OBS server

Default value:

None

indicator

str

Explanation:

Error indicator returned by the OBS server

Default value:

None

hostId

str

Explanation:

Requested server ID. If the value of status is less than 300, this parameter is left blank.

Default value:

None

resource

str

Explanation:

Error source (a bucket or an object). If the value of status is less than 300, this parameter is left blank.

Default value:

None

header

list

Explanation:

Response header list, composed of tuples. Each tuple consists of two elements, respectively corresponding to the key and value of a response header.

Default value:

None

body

object

Explanation:

Result content returned after the operation is successful. If the value of status is larger than 300, this parameter is left blank. The value varies with the API being called. For details, see the sections "Bucket-Related APIs" and "Object-Related APIs".

Default value:

None

Code Examples

try:
    from obs import Notification, TopicConfiguration,FilterRule, EventType
     
    fr1 = FilterRule(name='prefix', value='smn') 
    fr2 = FilterRule(name='suffix', value='.jpg') 
    topicConfiguration= TopicConfiguration(id='001',topic='your topic',events=[EventType.OBJECT_CREATED_ALL], filterRules=[fr1,fr2]) 
    
   
    resp = obsClient.setBucketNotification('bucketname', Notification(topicConfigurations=[topicConfiguration]))

    if resp.status < 300: 
        print('requestId:', resp.requestId) 
    else: 
        print('errorCode:', resp.errorCode) 
        print('errorMessage:', resp.errorMessage)
except:
    import traceback
    print(traceback.format_exc())