Boto3 Mavens,
What is the functional difference, if any, between Clients and Resources?
Are they functionally equivalent?
Under what conditions would you elect to invoke a Boto3 Resource vs. a Client (and vice-versa)?
Although I've endeavored to answer this question by RTM...regrets, understanding the functional difference between the two eludes me.
Your thoughts?
Many, many thanks!
Plane Wryter
Clients vs Resources 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.
Resources represent an object-oriented interface to Amazon Web Services (AWS). They provide a higher-level abstraction than the raw, low-level calls made by service clients. To use resources, you invoke the resource() method of a Session and pass in a service name: # Get resources from the default session sqs = boto3.
With aioboto3 you can now use the higher level APIs provided by boto3 in an asynchronous manner.
Boto3 generates the client from a JSON service definition file. The client's methods support every single type of interaction with the target AWS service. Resources, on the other hand, are generated from JSON resource definition files.
Resources are just a resource-based abstraction over the clients. They can't do anything the clients can't do, but in many cases they are nicer to use. They actually have an embedded client that they use to make requests. The downside is that they don't always support 100% of the features of a service.
Always create a resource
. It has the important methods you will need, such as Table
. If you happen to need a client
object, it's there ready for use, just ask for .meta.client
:
import boto3 dynamodb = boto3.resource(service_name='dynamodb', endpoint_url='http://localhost:8000') try: dynamodb.create_table(...) except dynamodb.meta.client.exceptions.ResourceInUseException: logging.warn('Table already exists') table = dynamodb.Table(table_name) response = table.get_item(...)
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