File: events.rst

package info (click to toggle)
python-boto3 1.26.27%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,880 kB
  • sloc: python: 12,629; makefile: 128
file content (424 lines) | stat: -rw-r--r-- 14,827 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
Extensibility guide
===================

All of Boto3's resource and client classes are generated at runtime.
This means that you cannot directly inherit and then extend the
functionality of these classes because they do not exist until the
program actually starts running.


However it is still possible to extend the functionality of classes through
Boto3's event system.


An introduction to the event system
-----------------------------------

Boto3's event system allows users to register a function to
a specific event. Then once the running program reaches a line that
emits that specific event, Boto3 will call every function
registered to the event in the order in which they were registered.
When Boto3 calls each of these registered functions,
it will call each of them with a specific set of
keyword arguments that are associated with that event.
Then once the registered function
is called, the function may modify the keyword arguments passed to that
function or return a value.
Here is an example of how the event system works::

    import boto3

    s3 = boto3.client('s3')

    # Access the event system on the S3 client
    event_system = s3.meta.events

    # Create a function 
    def add_my_bucket(params, **kwargs):
        # Add the name of the bucket you want to default to.
        if 'Bucket' not in params:
            params['Bucket'] = 'mybucket'

    # Register the function to an event
    event_system.register('provide-client-params.s3.ListObjects', add_my_bucket)

    response = s3.list_objects()

In this example, the handler ``add_my_bucket``
is registered such that the handler will inject the
value ``'mybucket`` for the ``Bucket`` parameter whenever the
``list_objects`` client call is made without the ``Bucket`` parameter. Note
that if the same ``list_objects`` call is made without the ``Bucket``
parameter and the registered handler, it will result in a validation error.

Here are the takeaways from this example:

* All clients have their own event system that you can use to fire events
  and register functions. You can access the event system through the
  ``meta.events`` attribute on the client.
* All functions registered to the event system must have ``**kwargs`` in
  the function signature. This is because emitting an event can have any
  number of keyword arguments emitted alongside it, and so if your
  function is called without ``**kwargs``, its signature will have to
  match every keyword argument emitted by the event. This also allows for
  more keyword arguments to be added to the emitted event in the future
  without breaking existing handlers.
* To register a function to an event, call the ``register`` method on the
  event system with the name of the event you want to register the
  function to and the function handle. Note that if you register the event
  after the event is emitted, the function will not be called unless the
  event is emitted again. In the example, the ``add_my_bucket`` handler
  was registered to the ``'provide-client-params.s3.ListObjects'`` event,
  which is an event that can be used to inject and modify parameters passed
  in by the client method. To read more about the event refer to
  `provide-client-params`_


A hierarchical structure
------------------------

The event system also provides a hierarchy for registering events such that
you can register a function to a set of events depending on the event name
hierarchy.

An event name can have its own hierarchy by specifying ``.`` in its name. For
example, take the event name ``'general.specific.more_specific'``. When
this event is emitted, the registered functions will be called in the order
from most specific to least specific registration. So in this example, the
functions will be called in the following order:

1) Functions registered to ``'general.specific.more_specific'``
2) Functions registered to ``'general.specific'``
3) Functions registered to ``'general'``

Here is a deeper example of how the event system works with respect to
its hierarchical structure::

    import boto3

    s3 = boto3.client('s3')

    # Access the event system on the S3 client
    event_system = s3.meta.events

    def add_my_general_bucket(params, **kwargs):
        if 'Bucket' not in params:
            params['Bucket'] = 'mybucket'

    def add_my_specific_bucket(params, **kwargs):
        if 'Bucket' not in params:
            params['Bucket'] = 'myspecificbucket'

    event_system.register('provide-client-params.s3', add_my_general_bucket)
    event_system.register('provide-client-params.s3.ListObjects', add_my_specific_bucket)

    list_obj_response = s3.list_objects()
    put_obj_response = s3.put_object(Key='mykey', Body=b'my body')

In this example, the ``list_objects`` method call will use the
``'myspecificbucket'`` for the bucket instead of ``'mybucket'`` because
the ``add_my_specific_bucket`` method was registered to the
``'provide-client-params.s3.ListObjects'`` event which is more specific than
the ``'provide-client-params.s3'`` event. Thus, the
``add_my_specific_bucket`` function is called before the
``add_my_general_bucket`` function is called when the event is emitted.

However for the ``put_object`` call, the bucket used is ``'mybucket'``. This
is because the event emitted for the ``put_object`` client call is
``'provide-client-params.s3.PutObject'`` and the ``add_my_general_bucket``
method is called via its registration to ``'provide-client-params.s3'``. The
``'provide-client-params.s3.ListObjects'`` event is never emitted so the
registered ``add_my_specific_bucket`` function is never called.


Wildcard matching
-----------------

Another aspect of Boto3's event system is that it has the capability
to do wildcard matching using the ``'*'`` notation. Here is an example
of using wildcards in the event system::

    import boto3

    s3 = boto3.client('s3')

    # Access the event system on the S3 client
    event_system = s3.meta.events

    def add_my_wildcard_bucket(params, **kwargs):
        if 'Bucket' not in params:
            params['Bucket'] = 'mybucket'

    event_system.register('provide-client-params.s3.*', add_my_wildcard_bucket)
    response = s3.list_objects()


The ``'*'`` allows you to register to a group of events without having to
know the actual name of the event. This is useful when you have to apply
the same handler in multiple places. Also note that if the wildcard is used,
it must be isolated. It does not handle globbing with additional characters.
So in the previous example, if the ``my_wildcard_function`` was registered
to ``'provide-client-params.s3.*objects'``, the handler would not be
called because it will consider ``'provide-client-params.s3.*objects'`` to be
a specific event.

The wildcard also respects the hierarchical structure of the event system.
If another handler was registered to the ``'provide-client-params.s3'`` event,
the ``add_my_wildcard_bucket`` would be called first because it is registered
to ``'provide-client-params.s3.*'`` which is more specific than the event
``'provide-client.s3'``.


Isolation of event systems
--------------------------

The event system in Boto3 has the notion of isolation:
all clients maintain their own set of registered handlers. For example if a
handler is registered to one client's event system, it will not be registered
to another client's event system::

    import boto3

    client1 = boto3.client('s3')
    client2 = boto3.client('s3')

    def add_my_bucket(params, **kwargs):
        if 'Bucket' not in params:
            params['Bucket'] = 'mybucket'

    def add_my_other_bucket(params, **kwargs):
        if 'Bucket' not in params:
            params['Bucket'] = 'myotherbucket'

    client1.meta.events.register(
        'provide-client-params.s3.ListObjects', add_my_bucket)
    client2.meta.events.register(
        'provide-client-params.s3.ListObjects', add_my_other_bucket)

    client1_response = client1.list_objects()
    client2_response = client2.list_objects()


Thanks to the isolation of clients' event systems, ``client1`` will inject
``'mybucket'`` for its ``list_objects`` method call while ``client2`` will
inject ``'myotherbucket'`` for its ``list_objects`` method call because
``add_my_bucket`` was registered to ``client1`` while ``add_my_other_bucket``
was registered to ``client2``.


Boto3 specific events
---------------------

Boto3 emits a set of events that users can register to
customize clients or resources and modify the behavior of method calls.

Here is the list of events that users of Boto3 can register handlers to:

* ``'creating-client-class'``
* ``'creating-resource-class'``
* ``'provide-client-params'``


`creating-client-class`
~~~~~~~~~~~~~~~~~~~~~

:Full Event Name:
  ``'creating-client-class.service-name'``

  Note: ``service-name`` refers to the value used to instantiate a client i.e.
  ``boto3.client('service-name')``

:Description:
  This event is emitted upon creation of the client class for a service. The
  client class for a service is not created until the first instantiation of
  the client class. Use this event for adding methods to the client class
  or adding classes for the client class to inherit from.

:Keyword Arguments Emitted:

  :type class_attributes: dict
  :param class_attributes: A dictionary where the keys are the names of the
     attributes of the class and the values are the actual attributes of
     the class.

  :type base_classes: list
  :param base_classes: A list of classes that the client class will inherit
     from where the order of inheritance is the same as the order of the list.

:Expected Return Value: Do not return anything.

:Example:
  Here is an example of how to add a method to the client class::

    from boto3.session import Session
    
    def custom_method(self):
        print('This is my custom method')

    def add_custom_method(class_attributes, **kwargs):
        class_attributes['my_method'] = custom_method

    session = Session()
    session.events.register('creating-client-class.s3', add_custom_method)

    client = session.client('s3')
    client.my_method()

  This should output::

    This is my custom method
    

  Here is an example of how to add a new class for the client class to
  inherit from::

    from boto3.session import Session

    class MyClass(object):
        def __init__(self, *args, **kwargs):
            super(MyClass, self).__init__(*args, **kwargs)
            print('Client instantiated!')

    def add_custom_class(base_classes, **kwargs):
        base_classes.insert(0, MyClass)

    session = Session()
    session.events.register('creating-client-class.s3', add_custom_class)

    client = session.client('s3')

  This should output::

    Client instantiated!


`creating-resource-class`
~~~~~~~~~~~~~~~~~~~~~~~

:Full Event Name:
  ``'creating-resource-class.service-name.resource-name'``

  Note: ``service-name`` refers to the value used to instantiate a service
  resource i.e. ``boto3.resource('service-name')`` and ``resource-name``
  refers to the name of the resource class.

:Description:
  This event is emitted upon creation of the resource class. The
  resource class is not created until the first instantiation of
  the resource class. Use this event for adding methods to the resource
  class or adding classes for the resource class to inherit from.

:Keyword Arguments Emitted:

  :type class_attributes: dict
  :param class_attributes: A dictionary where the keys are the names of the
     attributes of the class and the values are the actual attributes of
     the class.

  :type base_classes: list
  :param base_classes: A list of classes that the resource class will inherit
     from where the order of inheritance is the same as the order of the list.

:Expected Return Value: Do not return anything.

:Example:
  Here is an example of how to add a method to a resource class::

    from boto3.session import Session
    
    def custom_method(self):
        print('This is my custom method')

    def add_custom_method(class_attributes, **kwargs):
        class_attributes['my_method'] = custom_method

    session = Session()
    session.events.register('creating-resource-class.s3.ServiceResource',
                            add_custom_method)

    resource = session.resource('s3')
    resource.my_method()

  This should output::

    This is my custom method
    

  Here is an example of how to add a new class for a resource class to
  inherit from::

    from boto3.session import Session

    class MyClass(object):
        def __init__(self, *args, **kwargs):
            super(MyClass, self).__init__(*args, **kwargs)
            print('Resource instantiated!')

    def add_custom_class(base_classes, **kwargs):
        base_classes.insert(0, MyClass)

    session = Session()
    session.events.register('creating-resource-class.s3.ServiceResource',
                            add_custom_class)

    resource = session.resource('s3')

  This should output::

    Resource instantiated!


`provide-client-params`
~~~~~~~~~~~~~~~~~~~~~

:Full Event Name:
  ``'provide-client-params.service-name.operation-name'``

  Note: ``service-name`` refers to the value used to instantiate a client i.e.
  ``boto3.client('service-name')``. ``operation-name`` refers to the
  underlying API operation of the corresponding client method. To access
  the operation API name, retrieve the value from the
  ``client.meta.method_to_api_mapping`` dictionary using the name of the
  desired client method as the key.

:Description:
  This event is emitted before validation of the parameters passed to
  client method. Use this event to inject or modify parameters prior
  to the parameters being validated and built into a request that is sent
  over the wire.

:Keyword Arguments Emitted:

  :type params: dict
  :param params: A dictionary where the keys are the names of the
    parameters passed through the client method and the values are the values
    of those parameters.

  :type model: ``botocore.model.OperationModel``
  :param model: A model representing the underlying API operation of the
    client method.

:Expected Return Value: Do not return anything or return a new dictionary of
  parameters to use when making the request.

:Example:
  Here is an example of how to inject a parameter using the event::

    import boto3

    s3 = boto3.client('s3')

    # Access the event system on the S3 client
    event_system = s3.meta.events

    # Create a function
    def add_my_bucket(params, **kwargs):
        # Add the name of the bucket you want to default to.
        if 'Bucket' not in params:
            params['Bucket'] = 'mybucket'

    # Register the function to an event
    event_system.register('provide-client-params.s3.ListObjects', add_my_bucket)

    response = s3.list_objects()