Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 770
Plane Wryter Avatar asked Jul 30 '16 04:07

Plane Wryter


People also ask

What is the difference between boto3 client and boto3 resource?

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.

What is resource in boto3?

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.

Is boto3 asynchronous?

With aioboto3 you can now use the higher level APIs provided by boto3 in an asynchronous manner.

How does boto3 client work?

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.


2 Answers

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.

like image 109
Jordon Phillips Avatar answered Sep 19 '22 17:09

Jordon Phillips


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(...) 
like image 26
hlidka Avatar answered Sep 18 '22 17:09

hlidka