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 (at most 1000**):
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'])
** 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.
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.
You can see that the
Resource version of the code is much simpler, more compact, and has more capability (it does pagination for you). The
Client version of the code would actually be more complicated than shown above if you wanted to include pagination.
A useful resource to learn more about these boto3 concepts is the introductory re:Invent video.
~ Answered on 2018-02-19 14:00:41