Difference in boto3 between resource, client, and session?
I am using Python 2.7.12 in Ubuntu 16.04 LTS. I'm learning how to use boto3 from the following link: 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.
To add to the other answer, specifically when comparing Client vs. Resource.
- low-level service access
- generated from service description
- exposes botocore client to the developer
- typically maps 1:1 with the service API
- snake-cased method names (e.g. ListBuckets API => list_buckets method)
Here's an example of client-level access to an S3 bucket's objects (at most 1000**):
import boto3 client = boto3.client('s3') response = client.list_objects(Bucket='mybucket') for content in response['Contents']: obj_dict = client.get_object(Bucket='mybucket', Key=content['Key']) print(content['Key'], obj_dict['LastModified'])
** you would have to use a paginator, or implement your own loop, calling list_objects() repeatedly with a continuation marker if there were more than 1000.
- higher-level, object-oriented API
- generated from resource description
- uses identifiers and attributes
- has actions (operations on resources)
- exposes subresources and collections
Here's the equivalent example using resource-level access to an S3 bucket's objects (all):
import boto3 s3 = boto3.resource('s3') bucket = s3.Bucket('mybucket') for obj in bucket.objects.all(): print(obj.key, obj.last_modified)
Note that 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 subresources are lazily-loaded.
- stores configuration information (primarily credentials and selected region)
- allows you to create service clients and resources
A useful resource to learn more about these boto3 concepts is the introductory re:Invent video.