50

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)?

user438383
  • 5,716
  • 8
  • 28
  • 43
Plane Wryter
  • 1,299
  • 1
  • 11
  • 14

2 Answers2

24

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.

Jordon Phillips
  • 14,963
  • 4
  • 35
  • 42
  • 6
    I feel like if an answer just rearranges the terminology in a terminology question, it's not really adding much value. I can see that one is a "client" and one is a "resource" and that they each have different API's. Why did the designers of boto3 create these two API's, and how did they distinguish which is which. This would help answer the "Under which conditions would you elect to invoke a resource vs a client?" portion of the OP's question. – Anthony Manning-Franklin Jul 21 '17 at 05:42
2

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(...)
hlidka
  • 2,086
  • 1
  • 15
  • 14