boto3 Get a resource from a client

六月ゝ 毕业季﹏ 提交于 2021-02-07 19:06:26

问题


The AWS Library for python (boto) has two different types of interfaces for working with AWS, a low level client and a higher level more pythonic resource.

Parts of my code use one, while other parts use the other.

Getting a client from a resource is found from the docs.

# Create the resource
sqs_resource = boto3.resource('sqs')

# Get the client from the resource
sqs = sqs_resource.meta.client

My questions is if have the client sqs, how do I get a boto3.resource from this?

(I can't simply just call boto3.resource('sqs') because the client has other things, such as credentials already attached to it, the resource for some design reason tries to fetch the AWS credentials from a bunch of places I don't want it to, I'd like it to use whatever credentials/account is set on the client)


回答1:


There is no way to do this. If you want to use both, you should create a resource and use the embedded client. You can instantiate a resource with the exact same configuration as a client. The underlying client for the resource is created in the exact same way. The only difference between a resource's client and a client created with the exact same parameters is that the resource client adds 'Resource' to the user-agent.




回答2:


I think you should create resource and client separately as below:

import boto3
sqs_resource = boto3.resource("sqs")
sqs_client = boto3.client("sqs")

print dir(sqs_resource)
print dir(sqs_client)

Output:

[u'Message', u'Queue', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', u'create_queue', 'get_available_subresources', u'get_queue_by_name', 'meta', u'queues']
['_PY_TO_OP_NAME', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_cache', '_client_config', '_convert_to_request_dict', '_endpoint', '_force_path_style_s3_addressing', '_force_virtual_style_s3_addressing', '_get_waiter_config', '_loader', '_make_api_call', '_register_handlers', '_register_s3_specific_handlers', '_request_signer', '_response_parser', '_serializer', '_service_model', u'add_permission', 'can_paginate', u'change_message_visibility', u'change_message_visibility_batch', u'create_queue', u'delete_message', u'delete_message_batch', u'delete_queue', 'generate_presigned_url', 'get_paginator', u'get_queue_attributes', u'get_queue_url', 'get_waiter', u'list_dead_letter_source_queues', u'list_queues', 'meta', u'purge_queue', u'receive_message', u'remove_permission', u'send_message', u'send_message_batch', u'set_queue_attributes', 'waiter_names']

From above output, you will always get client from resource as sqs_resource.meta.client.

But vice-versa is not possible.

Instead, create resource and client both and use whatever you required. Please let me know if this is useful.



来源:https://stackoverflow.com/questions/38581465/boto3-get-a-resource-from-a-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!