Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in boto3 between resource, client, and session?

Tags:

python

boto3

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.

like image 235
shiva Avatar asked Mar 15 '17 11:03

shiva


People also ask

What is the difference between client and resource in Boto3?

00:00 Boto3's primary function is to make AWS API calls for you. It extracts these APIs in two main ways: clients and resources. Clients give you low-level service access, while resources provide an object-oriented way of working with these services. 00:43 and set that equal to boto3.

What is session in Boto3?

The boto3. Session class, according to the docs, “ stores configuration state and allows you to create service clients and resources.” Most importantly it represents the configuration of an IAM identity (IAM user or assumed role) and AWS region, the two things you need to talk to an AWS service.

What is resource in Boto3?

Resources represent an object-oriented interface to Amazon Web Services (AWS). They provide a higher-level abstraction than the raw, low-level calls made by service clients. To use resources, you invoke the resource() method of a Session and pass in a service name: # Get resources from the default session sqs = boto3.

What are Boto3 clients?

Boto3 client is a low-level service class to connect to AWS service. It provides similar methods available in the AWS API. All the methods available in the AWS API are available in the Boto3 client.


1 Answers

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 an application can use both, as needed. I've outlined the differences between Client and Resource 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.

Client:

  • this is the original boto3 API abstraction
  • it provides low-level AWS service access
  • all AWS service operations are supported by clients
  • it exposes botocore client to the developer
  • it typically maps 1:1 with the AWS service API
  • it exposes snake-cased method names (e.g. ListBuckets API => list_buckets method)
  • it is generated from an AWS service description

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.

Resource:

  • this is the newer boto3 API abstraction
  • it provides high-level, object-oriented API
  • it does not provide 100% API coverage of AWS services
  • it uses identifiers and attributes
  • it has actions (operations on resources)
  • it exposes sub-resources and collections of AWS resources
  • it is generated from an AWS resource description

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: 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.

Session:

  • stores configuration information (primarily credentials and selected region)
  • allows you to create service clients and resources
  • boto3 creates a default session for you when needed

A useful resource to learn more about these boto3 concepts is the introductory re:Invent video.

like image 75
jarmod Avatar answered Nov 08 '22 13:11

jarmod