Python 资源、客户端和会话之间 boto3 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42809096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference in boto3 between resource, client, and session?
提问by shiva
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.
我在 Ubuntu 16.04 LTS 中使用 Python 2.7.12。我正在通过以下链接学习如何使用 boto3:https://boto3.readthedocs.io/en/latest/guide/quickstart.html#using-boto-3 。我的疑问是何时使用资源、客户端或会话,以及它们各自的功能。
回答by jarmod
Here's some more detailed information on what Client, Resource, and Sessionare all about.
以下是有关Client、Resource和Session的更多详细信息。
Client:
客户:
- low-level AWS service access
- generated from AWS servicedescription
- exposes botocore client to the developer
- typically maps 1:1 with the AWS service API
- all AWS service operations are supported by clients
- snake-cased method names (e.g. ListBuckets API => list_buckets method)
- 低级 AWS 服务访问
- 从 AWS服务描述生成
- 向开发人员公开 botocore 客户端
- 通常与 AWS 服务 API 1:1 映射
- 客户端支持所有 AWS 服务操作
- 蛇形方法名称(例如 ListBuckets API => list_buckets 方法)
Here's an example of client-level access to an S3 bucket's objects (at most 1000**):
以下是对 S3 存储桶对象(最多 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.
** 您将不得不使用分页器,或者实现自己的循环,如果超过 1000 个,则使用延续标记重复调用 list_objects()。
Resource:
资源:
- higher-level, object-oriented API
- generated from resourcedescription
- uses identifiers and attributes
- has actions (operations on resources)
- exposes subresources and collections of AWS resources
- does not provide 100% API coverage of AWS services
- 更高级的、面向对象的 API
- 从资源描述生成
- 使用标识符和属性
- 有操作(对资源的操作)
- 公开 AWS 资源的子资源和集合
- 不提供 AWS 服务的 100% API 覆盖率
Here's the equivalent example using resource-level access to an S3 bucket's objects (all):
下面是使用资源级访问 S3 存储桶对象(全部)的等效示例:
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.
请注意,在这种情况下,您不必进行第二次 API 调用来获取对象;它们可以作为存储桶上的集合提供给您。这些子资源集合是延迟加载的。
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.
您可以看到该Resource
版本的代码更简单、更紧凑且功能更多(它为您进行分页)。Client
如果您想包含分页,代码的版本实际上会比上面显示的更复杂。
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
- 存储配置信息(主要是凭据和所选区域)
- 允许您创建服务客户端和资源
- boto3 在需要时为您创建一个默认会话
A useful resource to learn more about these boto3 concepts is the introductory re:Invent video.
了解有关这些 boto3 概念的更多信息的有用资源是介绍性 re:Invent 视频。
回答by mootmoot
I'll try and explain it as simple as possible. So there is no guarantee of the accuracy of the actual terms.
我会尝试尽可能简单地解释它。因此无法保证实际条款的准确性。
Sessionis where to initiate the connectivity to AWS services. E.g. following is default session that uses the default credential profile(e.g. ~/.aws/credentials, or assume your EC2 using IAM instance profile )
会话是启动到 AWS 服务的连接的地方。例如,以下是使用默认凭证配置文件的默认会话(例如 ~/.aws/credentials,或假设您的 EC2 使用 IAM 实例配置文件)
sqs = boto3.client('sqs')
s3 = boto3.resource('s3')
Because default session is limit to the profile or instance profile used, sometimes you need to use the custom session to override the default session configuration (e.g. region_name, endpoint_url, etc. ) e.g.
由于默认会话仅限于使用的配置文件或实例配置文件,有时您需要使用自定义会话来覆盖默认会话配置(例如 region_name、endpoint_url 等)例如
# custom resource session must use boto3.Session to do the override
my_west_session = boto3.Session(region_name = 'us-west-2')
my_east_session = boto3.Session(region_name = 'us-east-1')
backup_s3 = my_west_session.resource('s3')
video_s3 = my_east_session.resource('s3')
# you have two choices of create custom client session.
backup_s3c = my_west_session.client('s3')
video_s3c = boto3.client("s3", region_name = 'us-east-1')
Resource: This is the high-level service class recommended to be used. This allows you to tied particular AWS resources and passes it along, so you just use this abstraction than worry which target services are pointed to. As you notice from the session part, if you have a custom session, you just pass this abstract object than worrying about all custom regions,etc to pass along. Following is a complicated example E.g.
Resource:这是推荐使用的高级服务类。这允许您绑定特定的 AWS 资源并将其传递,因此您只需使用此抽象,而不必担心指向哪些目标服务。正如您在会话部分注意到的那样,如果您有一个自定义会话,您只需传递这个抽象对象,而不必担心所有自定义区域等要传递。下面是一个复杂的例子
import boto3
my_west_session = boto3.Session(region_name = 'us-west-2')
my_east_session = boto3.Session(region_name = 'us-east-1')
backup_s3 = my_west_session.resource("s3")
video_s3 = my_east_session.resource("s3")
backup_bucket = backup_s3.Bucket('backupbucket')
video_bucket = video_s3.Bucket('videobucket')
# just pass the instantiated bucket object
def list_bucket_contents(bucket):
for object in bucket.objects.all():
print(object.key)
list_bucket_contents(backup_bucket)
list_bucket_contents(video_bucket)
Clientis a low level class object. For each client call, you need to explicitly specify the targeting resources, the designated service target name must be pass long. You will lose the abstraction ability.
客户端是一个低级类对象。对于每个客户端调用,都需要明确指定目标资源,指定的服务目标名称必须是pass long。你将失去抽象能力。
For example, if you only deal with the default session, this looks similar to boto3.resource.
例如,如果您只处理默认会话,这看起来类似于 boto3.resource。
import boto3
s3 = boto3.client('s3')
def list_bucket_contents(bucket_name):
for object in s3.list_objects_v2(Bucket=bucket_name) :
print(object.key)
list_bucket_contents('Mybucket')
However, if you want to list objects from a bucket in different regions, you need to specify the explicit bucket parameter required for the client.
但是,如果要列出不同地域的桶中的对象,则需要指定客户端所需的显式桶参数。
import boto3
backup_s3 = my_west_session.client('s3',region_name = 'us-west-2')
video_s3 = my_east_session.client('s3',region_name = 'us-east-1')
# you must pass boto3.Session.client and the bucket name
def list_bucket_contents(s3session, bucket_name):
response = s3session.list_objects_v2(Bucket=bucket_name)
if 'Contents' in response:
for obj in response['Contents']:
print(obj['key'])
list_bucket_contents(backup_s3, 'backupbucket')
list_bucket_contents(video_s3 , 'videobucket')