用法
将属于客户端对象的方法修饰为单个会话
session=boto3.session.Session()session.register_client_decorator(service_name,method_names,decorator)
参数:
- 服务名称——要应用装饰程序的客户端的boto3名称(作为字符串)。
- method_names——要应用decorator的客户端的一个或多个方法名。单个名称可以是字符串,而多个名称可以是列表/元组/集。
- decorator——decorator函数。必须是接受函数并返回函数的函数。返回的函数必须以(*args,**kwargs)作为参数。
在单个会话中修饰属于资源对象的方法
session=boto3.session.Session()session.register_resource_decorator(service_name,resource_name,method_names,decorator)
参数:
- 服务名称——要应用装饰程序的服务的boto3名称(作为字符串)。
- resource_name——要应用装饰程序的服务的资源的boto3名称。
- 方法名称——要应用装饰程序的资源的一个或多个方法名称。单个名称可以是字符串,而多个名称应该是列表/元组/集。
- decorator——decorator函数。必须是接受函数并返回函数的函数。返回的函数必须采用(*args,**kwargs)作为参数
为在任何会话中创建的客户端修饰方法
boto3.session.Session.add_client_decorator(service_name,method_names,decorator)
参数:
- 服务名称——要应用装饰程序的客户端的boto3名称(作为字符串)。
- method_names——要应用decorator的客户端的一个或多个方法名。单个名称可以是字符串,而多个名称应该是列表/元组/集。
- decorator——decorator函数。必须是接受函数并返回函数的函数。返回的函数必须以(*args,**kwargs)作为参数。
在所有会话中修饰资源的方法
boto3.session.Session.add_resource_decorator(service_name,resource_name,method_names,decorator)
参数:
- 服务名称——要应用装饰程序的服务的Boto3名称。
- resource_name——要应用装饰程序的服务的资源的boto3名称。
- 方法名称——要应用装饰程序的资源的一个或多个方法名称。单个名称可以是字符串,而多个名称应该是列表/元组/集
- decorator——decorator函数。必须是接受函数并返回函数的函数。返回的函数必须采用(*args,**kwargs)作为参数
注销装饰程序,以便将来的客户机不会对其方法进行装饰。已向Methods注册装饰者的客户将保留其装饰。
session=boto3.session.Session()session.unregister_client_decorator(service_name,method_names)
参数:
- 服务名称——要应用装饰程序的服务的Boto3名称。
- method_names——要应用decorator的客户端的一个或多个方法名。单个名称可以是字符串,而多个名称应该是列表/元组/集
取消注册装饰程序,以便将来的资源不会对其方法进行装饰。已将装饰者注册到方法的资源将保留其装饰。
session=boto3.session.Session()session.unregister_resource_decorator(service_name,resource_name,method_names)
参数:
- 服务名称——要应用装饰程序的服务的Boto3名称。
- resource_name——要应用装饰程序的服务的资源的boto3名称。
- 方法名称——要应用装饰程序的资源的一个或多个方法名称。单个名称可以是字符串,而mu多个名称应为列表/元组/集
为在任何会话中创建的客户端取消修饰方法。
boto3.session.Session.remove_client_decorator(service_name,method_names)
参数:
- 服务名称——要应用装饰程序的客户端的boto3名称(作为字符串)。
- method_names——要应用decorator的客户端的一个或多个方法名。单个名称可以是字符串,而多个名称应该是列表/元组/集。
在所有会话中取消装饰资源的方法
boto3.session.Session.remove_resource_decorator(service_name,resource_name,method_names)
参数:
- 服务名称——要应用装饰程序的服务的Boto3名称。
- resource_name——要应用装饰程序的服务的资源的boto3名称。
- 方法名称——要应用装饰程序的资源的一个或多个方法名称。单个名称可以是字符串,而多个名称应该是列表/元组/集
BOTO3方便方法
如果您使用boto3.client()
或boto3.resource()
方法,这些方法将创建一个默认会话对象,该对象位于boto3.default\u session
中。
更改默认会话的decorators需要使用这里记录的register_xxx
和unregister_xxx
方法。
例如boto3.default_session.register_client_decorator(…)
在单个boto3会话上装饰create_bucket()的示例
importboto3importbotoinatorfrommotoimportmock_s3,mock_sqs""" This is our decorator that we will apply to boto3 methods """defmyDecorator(func):deftest_decorator(*args,**kwargs):setattr(test_decorator,'testValue',True)# Add this attribute to the returned function for testingreturnfunc(*args,**kwargs)returntest_decorator@mock_s3deftestRegisterToClient():""" Test registering a decorator to a single boto3 session """# Create a boto3 sessions=boto3.session.Session()# Register the create_bucket() method to use our decorator for this sessions.register_client_decorator('s3','create_bucket',myDecorator)# Now create our client as we normally wouldclient1=s.client('s3')# Now we can see that create_bucket() was decorated by testing the attribute we addedclient1.create_bucket(Bucket='foo')asserthasattr(client1.create_bucket,'testValue')# We can also see that this only applies to calls made by the session we registered by creating a new session through boto3.client() and not registering a decoratorclient2=boto3.client('s3')client2.create_bucket(Bucket='foo')# Now we can see that client.create_bucket() is not decoratedassertnothasattr(client2.create_bucket,'testValue')# Remove the decorator from the sessions.unregister_client_decorator('s3','create_bucket')# Now create a new client on the same session we created at firstclient3=s.client('s3')client3.create_bucket(Bucket='bar')# The session should no longer be decorating methods for new clientsassertnothasattr(client3.create_bucket,'testValue1')
欢迎加入QQ群-->: 979659372
推荐PyPI第三方库