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)
To summarize, resources are higher-level abstractions of AWS services compared to clients. Resources are the recommended pattern to use boto3 as you don't have to worry about a lot of the underlying details when interacting with AWS services. As a result, code written with Resources tends to be simpler.
Boto3 resource is a high-level object-oriented API service you can use to connect and access your AWS resource. It has actions() defined which can be used to make calls to the AWS service.
00:00 Boto3's primary function is to make AWS API calls for you. It extracts these APIs in two main ways: clients and resources. Clients give you low-level service access, while resources provide an object-oriented way of working with these services.
Boto 3: Resource vs Client 1 Introduction. AWS’ Boto3 library is used commonly to integrate Python applications with various AWS services. ... 2 Client. Clients provide a low-level interface to the AWS service. ... 3 Resource. Resources are a higher-level abstraction compared to clients. ... 4 Clients vs Resources. ...
AWS’ Boto3 library is used commonly to integrate Python applications with various AWS services. The two most commonly used features of boto3 are Clients and Resources. In this article, we will look into each one of these and explain how they work and when to use them. Clients provide a low-level interface to the AWS service.
importboto3# Create a low-level client with the service namesqs=boto3.client('sqs') It is also possible to access the low-level client from an existing resource: # Create the resourcesqs_resource=boto3.resource('sqs')# Get the client from the resourcesqs=sqs_resource.meta.client Service operations¶
In the example above, each thread would have its own Boto3 session and its own instance of the S3 resource. This is a good idea because resources contain shared data when loaded and calling actions, accessing properties, or manually loading or reloading the resource can modify this data.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With