Clients and Resources
At its core, all that Boto3 does is call AWS APIs on your behalf. For the majority of the AWS services, Boto3 offers two distinct ways of accessing these abstracted APIs:
- Client: low-level service access
- Resource: higher-level object-oriented service access
You can use either to interact with S3.
To connect to the low-level client interface, you must use Boto3’s client()
. You then pass in the name of the service you want to connect to, in this case, s3
:
import boto3
s3_client = boto3.client('s3')
To connect to the high-level interface, you’ll follow a similar approach, but use resource()
:
import boto3
s3_resource = boto3.resource('s3')
You’ve successfully connected to both versions, but now you might be wondering, “Which one should I use?”
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:18
If you wanted to make a client interface, you could go ahead and do something like import boto3
,
00:25
and then make that interface and call it something like s3_client
and set that equal to boto3.client()
and then pass in 's3'
. To make the resource interface, you could say something like s3_resource
00:43
and set that equal to boto3.resource()
and pass in 's3'
. You can see that from a Python point of view, creating each of these interfaces is pretty similar and you may be asking yourself, “Why would you want to use one or the other?” Because they’re lower level, clients will often require more programming to work with, and this is because they generally return a dictionary response that must be parsed. If you were to use a resource, Boto3 will do that parsing for you.
01:14 The clients may give you slightly better performance, but they also may give you less readable code.
01:20 A big advantage to using the clients is that they can interact with the AWS API in every single way that it allows. This means that sometimes you may have an operation that isn’t supported by the resource interface.
01:34
If you’re using resources and run into one of these issues, you don’t need to change all of your code. You can actually call the client interface off of the resource as needed using .meta.client
.
01:45
So, let’s say you had your s3_resource
. You can just call .meta.client
and then from here, call any client method that you need.
01:56
One example of this is something like .generate_presigned_url()
,
02:03 which will allow a user to have access to an object in a bucket for a given period of time without having AWS credentials. For the rest of this guide, we’re going to focus on the resource interface, but feel free to look into the client documentation if you feel like your project can benefit from lower-level API calls.
02:23 Now that you understand the distinction between the resource and client interfaces, it’s time to create your first bucket. I’ll see you in the next video.
Become a Member to join the conversation.