Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use a boto3 client and when to use a boto3 resource?

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.

like image 385
aquil.abdullah Avatar asked Sep 01 '16 13:09

aquil.abdullah


People also ask

What is Boto3 resource?

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.

What is Boto3 client in Python?

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.

What is Boto3 client (' S3 ')?

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

Do we need to close Boto3 client?

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.


1 Answers

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.

like image 171
mootmoot Avatar answered Oct 20 '22 09:10

mootmoot