@@ -14,17 +14,16 @@ Boto3's event system.
14
14
An introduction to the event system
15
15
-----------------------------------
16
16
17
- Boto3's event system allows users to register a function to
18
- a specific event. Then once the running program reaches a line that
19
- emits that specific event, Boto3 will call every function
20
- registered to the event in the order in which they were registered.
21
- When Boto3 calls each of these registered functions,
22
- it will call each of them with a specific set of
23
- keyword arguments that are associated with that event.
24
- Then once the registered function
25
- is called, the function may modify the keyword arguments passed to that
26
- function or return a value.
27
- Here is an example of how the event system works::
17
+ Boto3's event system allows users to register a function to a specific event.
18
+ Then once the running program reaches a line that emits that specific event,
19
+ Boto3 will call every function registered to the event in the order in which
20
+ they were registered.
21
+
22
+ When Boto3 calls each of these registered functions, it will call each of them
23
+ with a specific set of keyword arguments that are associated with that event.
24
+ Then once the registered function is called, the function may modify the
25
+ keyword arguments passed to that function or return a value. Here is an
26
+ example of how the event system works::
28
27
29
28
import boto3
30
29
@@ -37,19 +36,19 @@ Here is an example of how the event system works::
37
36
def add_my_bucket(params, **kwargs):
38
37
# Add the name of the bucket you want to default to.
39
38
if 'Bucket' not in params:
40
- params['Bucket'] = 'mybucket '
39
+ params['Bucket'] = 'amzn-s3-demo-bucket '
41
40
42
41
# Register the function to an event
43
42
event_system.register('provide-client-params.s3.ListObjectsV2', add_my_bucket)
44
43
45
44
response = s3.list_objects_v2()
46
45
47
- In this example, the handler ``add_my_bucket ``
48
- is registered such that the handler will inject the
49
- value `` 'mybucket' `` for the ``Bucket `` parameter whenever the
50
- ``list_objects_v2 `` client call is made without the `` Bucket `` parameter. Note
51
- that if the same `` list_objects_v2 `` call is made without the `` Bucket ``
52
- parameter and the registered handler, it will result in a validation error.
46
+ In this example, the handler ``add_my_bucket `` is registered such that the
47
+ handler will inject the value `` 'amzn-s3-demo-bucket' `` for the `` Bucket ``
48
+ parameter whenever the ``list_objects_v2 `` client call is made without the
49
+ ``Bucket `` parameter. Note that if the same `` list_objects_v2 `` call is made
50
+ without the `` Bucket `` parameter and the registered handler, it will result in
51
+ a validation error.
53
52
54
53
Here are the takeaways from this example:
55
54
@@ -103,11 +102,11 @@ its hierarchical structure::
103
102
104
103
def add_my_general_bucket(params, **kwargs):
105
104
if 'Bucket' not in params:
106
- params['Bucket'] = 'mybucket '
105
+ params['Bucket'] = 'amzn-s3-demo-bucket1 '
107
106
108
107
def add_my_specific_bucket(params, **kwargs):
109
108
if 'Bucket' not in params:
110
- params['Bucket'] = 'myspecificbucket '
109
+ params['Bucket'] = 'amzn-s3-demo-bucket2 '
111
110
112
111
event_system.register('provide-client-params.s3', add_my_general_bucket)
113
112
event_system.register('provide-client-params.s3.ListObjectsV2', add_my_specific_bucket)
@@ -116,17 +115,18 @@ its hierarchical structure::
116
115
put_obj_response = s3.put_object(Key='mykey', Body=b'my body')
117
116
118
117
In this example, the ``list_objects_v2 `` method call will use the
119
- ``'myspecificbucket ' `` for the bucket instead of `` 'mybucket' `` because
120
- the ``add_my_specific_bucket `` method was registered to the
121
- ``'provide-client-params.s3.ListObjectsV2' `` event which is more specific than
122
- the ``'provide-client-params.s3' `` event. Thus, the
118
+ ``'amzn-s3-demo-bucket2 ' `` for the bucket instead of
119
+ `` 'amzn-s3-demo-bucket1' `` because the ``add_my_specific_bucket `` method was
120
+ registered to the ``'provide-client-params.s3.ListObjectsV2' `` event which is
121
+ more specific than the ``'provide-client-params.s3' `` event. Thus, the
123
122
``add_my_specific_bucket `` function is called before the
124
123
``add_my_general_bucket `` function is called when the event is emitted.
125
124
126
- However for the ``put_object `` call, the bucket used is ``'mybucket' ``. This
127
- is because the event emitted for the ``put_object `` client call is
128
- ``'provide-client-params.s3.PutObject' `` and the ``add_my_general_bucket ``
129
- method is called via its registration to ``'provide-client-params.s3' ``. The
125
+ However for the ``put_object `` call, the bucket used is
126
+ ``'amzn-s3-demo-bucket1' ``. This is because the event emitted for the
127
+ ``put_object `` client call is ``'provide-client-params.s3.PutObject' `` and the
128
+ ``add_my_general_bucket `` method is called via its registration to
129
+ ``'provide-client-params.s3' ``. The
130
130
``'provide-client-params.s3.ListObjectsV2' `` event is never emitted so the
131
131
registered ``add_my_specific_bucket `` function is never called.
132
132
@@ -147,7 +147,7 @@ of using wildcards in the event system::
147
147
148
148
def add_my_wildcard_bucket(params, **kwargs):
149
149
if 'Bucket' not in params:
150
- params['Bucket'] = 'mybucket '
150
+ params['Bucket'] = 'amzn-s3-demo-bucket '
151
151
152
152
event_system.register('provide-client-params.s3.*', add_my_wildcard_bucket)
153
153
response = s3.list_objects_v2()
@@ -184,11 +184,11 @@ to another client's event system::
184
184
185
185
def add_my_bucket(params, **kwargs):
186
186
if 'Bucket' not in params:
187
- params['Bucket'] = 'mybucket '
187
+ params['Bucket'] = 'amzn-s3-demo-bucket1 '
188
188
189
189
def add_my_other_bucket(params, **kwargs):
190
190
if 'Bucket' not in params:
191
- params['Bucket'] = 'myotherbucket '
191
+ params['Bucket'] = 'amzn-s3-demo-bucket2 '
192
192
193
193
client1.meta.events.register(
194
194
'provide-client-params.s3.ListObjectsV2', add_my_bucket)
@@ -200,10 +200,10 @@ to another client's event system::
200
200
201
201
202
202
Thanks to the isolation of clients' event systems, ``client1 `` will inject
203
- ``'mybucket ' `` for its ``list_objects_v2 `` method call while `` client2 `` will
204
- inject ``'myotherbucket ' `` for its ``list_objects_v2 `` method call because
205
- ``add_my_bucket `` was registered to ``client1 `` while `` add_my_other_bucket ``
206
- was registered to ``client2 ``.
203
+ ``'amzn-s3-demo-bucket1 ' `` for its ``list_objects_v2 `` method call while
204
+ `` client2 `` will inject ``'amzn-s3-demo-bucket2 ' `` for its ``list_objects_v2 ``
205
+ method call because ``add_my_bucket `` was registered to ``client1 `` while
206
+ `` add_my_other_bucket `` was registered to ``client2 ``.
207
207
208
208
209
209
Boto3 specific events
@@ -212,13 +212,14 @@ Boto3 specific events
212
212
Boto3 emits a set of events that users can register to
213
213
customize clients or resources and modify the behavior of method calls.
214
214
215
- Here is a table of events that users of Boto3 can register handlers to. More information
216
- about each event can be found in the corresponding sections below:
215
+ Here is a table of events that users of Boto3 can register handlers to. More
216
+ information about each event can be found in the corresponding sections below:
217
217
218
218
.. note ::
219
219
220
- Events with a ``* `` in their order number are conditionally emitted while all others are always emitted.
221
- An explanation of all 3 conditional events is provided below.
220
+ Events with a ``* `` in their order number are conditionally emitted while
221
+ all others are always emitted. An explanation of all 3 conditional events is
222
+ provided below.
222
223
223
224
``2 * `` - ``creating-resource-class `` is emitted ONLY when using a service resource.
224
225
@@ -440,7 +441,7 @@ about each event can be found in the corresponding sections below:
440
441
def add_my_bucket(params, **kwargs):
441
442
# Add the name of the bucket you want to default to.
442
443
if 'Bucket' not in params:
443
- params['Bucket'] = 'mybucket '
444
+ params['Bucket'] = 'amzn-s3-demo-bucket '
444
445
445
446
# Register the function to an event
446
447
event_system.register('provide-client-params.s3.ListObjectsV2', add_my_bucket)
@@ -551,13 +552,13 @@ about each event can be found in the corresponding sections below:
551
552
# Register the function to an event
552
553
event_system.register('request-created.s3.ListObjectsV2', inspect_request_created)
553
554
554
- response = s3.list_objects_v2(Bucket='my -bucket')
555
+ response = s3.list_objects_v2(Bucket='amzn-s3-demo -bucket')
555
556
556
557
This should output::
557
558
558
559
Request Info:
559
560
method: GET
560
- url: https://my -bucket.s3 ...
561
+ url: https://amzn-s3-demo -bucket.s3 ...
561
562
data: ...
562
563
params: { ... }
563
564
auth_path: ...
@@ -682,9 +683,9 @@ about each event can be found in the corresponding sections below:
682
683
``'after-call.service-name.operation-name' ``
683
684
684
685
:Description:
685
- This event is emitted just after the service client makes an API call.
686
- This event allows developers to postprocess or inspect the API response according to the
687
- specific requirements of their application if needed.
686
+ This event is emitted just after the service client makes an API call. This
687
+ event allows developers to postprocess or inspect the API response according
688
+ to the specific requirements of their application if needed.
688
689
689
690
:Keyword Arguments Emitted:
690
691
@@ -720,7 +721,7 @@ about each event can be found in the corresponding sections below:
720
721
# Register the function to an event
721
722
event_system.register('after-call.s3.ListObjectsV2', print_after_call_args)
722
723
723
- s3.list_objects_v2(Bucket='my -bucket')
724
+ s3.list_objects_v2(Bucket='amzn-s3-demo -bucket')
724
725
725
726
This should output::
726
727
0 commit comments