When to use a boto3 client and when to use a boto3 resource?

Amazon Web-ServicesBoto3

Amazon Web-Services Problem Overview


I am trying to understand when I should use a Resource and when I should use a Client.

The definitions provided in boto3 docs don't really make it clear when it is preferable to use one or the other.

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

boto3.resource is a high-level services class wrap around boto3.client.

It is meant to attach connected resources under where you can later use other resources without specifying the original resource-id.

import boto3
s3 = boto3.resource("s3")
bucket = s3.Bucket('mybucket')

# now bucket is "attached" the S3 bucket name "mybucket"
print(bucket)
# s3.Bucket(name='mybucket')

print(dir(bucket))
#show you all class method action you may perform

OTH, boto3.client are low level, you don't have an "entry-class object", thus you must explicitly specify the exact resources it connects to for every action you perform.

It depends on individual needs. However, boto3.resource doesn't wrap all the boto3.client functionality, so sometime you need to call boto3.client , or use boto3.resource.meta.client to get the job done.

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
Questionaquil.abdullahView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesmootmootView Answer on Stackoverflow