Are Boto3 Resources and Clients Equivalent? When Use One or Other?

PythonAmazon Web-ServicesBoto3

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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(...)

    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPlane WryterView Question on Stackoverflow
Solution 1 - PythonJordon PhillipsView Answer on Stackoverflow
Solution 2 - PythonhlidkaView Answer on Stackoverflow