I am trying to understand when I should use a Resource and when I should use a Client.
The definitions provided in boto3 docs don't really make it clear when it is preferable to use one or the other.
Boto3 resource is a high-level object-oriented API service you can use to connect and access your AWS resource. It has actions() defined which can be used to make calls to the AWS service.
Boto3 is the name of the Python SDK for AWS. It allows you to directly create, update, and delete AWS resources from your Python scripts.
Boto3 is the official AWS SDK for Python, used to create, configure, and manage AWS services. The following are examples of defining a resource/client in boto3 for the Weka S3 service, managing credentials, and pre-signed URLs, generating secure temporary tokens, and using those to run S3 API calls.
Under the covers, boto uses httplib. This client library supports HTTP 1.1 Keep-Alive, so it can and should keep the socket open so that it can perform multiple requests over the same connection. connection. close() does not actually close the underlying sockets.
boto3.resource
is a high-level services class wrap around boto3.client
.
It is meant to attach connected resources under where you can later use other resources without specifying the original resource-id.
import boto3 s3 = boto3.resource("s3") bucket = s3.Bucket('mybucket') # now bucket is "attached" the S3 bucket name "mybucket" print(bucket) # s3.Bucket(name='mybucket') print(dir(bucket)) #show you all class method action you may perform
OTH, boto3.client are low level, you don't have an "entry-class object", thus you must explicitly specify the exact resources it connects to for every action you perform.
It depends on individual needs. However, boto3.resource
doesn't wrap all the boto3.client
functionality, so sometime you need to call boto3.client
, or use boto3.resource.meta.client
to get the job done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With