I'm learning how to use AWS SDK for Python (Boto3) from the following resource:
https://boto3.readthedocs.io/en/latest/guide/quickstart.html#using-boto-3.
My doubt is when to use resource
, client
, or session
, and their respective functionality.
I am using Python 2.7.12 in Ubuntu 16.04 LTS.
Client and Resource are two different abstractions within the boto3 SDK for making AWS service requests. If you want to make API calls to an AWS service with boto3, then you do so via a Client or a Resource.
You would typically choose to use either the Client abstraction or the Resource abstraction, but you can use both, as needed. I've outlined the differences below to help readers decide which to use.
Session is largely orthogonal to the concepts of Client and Resource (but is used by both).
Here's some more detailed information on what Client, Resource, and Session are all about.
Here's an example of client-level access to an S3 bucket's objects:
import boto3
client = boto3.client('s3')
response = client.list_objects_v2(Bucket='mybucket')
for content in response['Contents']:
obj_dict = client.get_object(Bucket='mybucket', Key=content['Key'])
print(content['Key'], obj_dict['LastModified'])
Note: this client-level code is limited to listing at most 1000 objects. You would have to use a paginator, or implement your own loop, calling list_objects_v2()
repeatedly with a continuation marker if there were more than 1000 objects.
OK, so that's the low-level Client interface. Now onto the higher-level (more abstract) Resource interface.
Here's the equivalent example using resource-level access to an S3 bucket's objects:
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')
for obj in bucket.objects.all():
print(obj.key, obj.last_modified)
Note: in this case you do not have to make a second API call to get the objects; they're available to you as a collection on the bucket. These collections of sub-resources are lazily-loaded.
You can see that the Resource
version of the code is much simpler, more compact, and has more capability (for example it does pagination for you and it exposes properties instead of a raw dictionary). The Client
version of the code would actually be more complicated than shown above if you wanted to include pagination.
Finally, onto Session which is fundamental to both Client and Resource and how both get access to AWS credentials, for example.
A useful resource to learn more about these boto3 concepts is the introductory re:Invent video.
Per the Resources page in the boto3 documentation:
The AWS Python SDK team does not intend to add new features to the resources interface in boto3. Existing interfaces will continue to operate during boto3's lifecycle. Customers can find access to newer service features through the client interface.
You can read more about the plans to maintain but no longer enhance resources at boto3/discussions/3563.