资源,客户端和会话之间的boto3差异? [英] Difference in boto3 between resource, client, and session?

查看:79
本文介绍了资源,客户端和会话之间的boto3差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Ubuntu 16.04 LTS中使用Python 2.7.12.我正在通过以下链接学习如何使用boto3: https://boto3.readthedocs.io/en/latest/guide/quickstart.html#using-boto-3 .我的疑问是何时使用资源,客户端或会话及其各自的功能.

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.

推荐答案

以下是有关资源会话都是关于

Here's some more detailed information on what Client, Resource, and Session are all about.

客户:

  • 低级AWS服务访问权限
  • 从AWS 服务说明生成
  • 向开发人员展示botocore客户端
  • 通常使用AWS服务API 1:1映射
  • 客户端支持所有AWS服务操作
  • 蛇形方法名称(例如ListBuckets API => list_buckets方法)
  • low-level AWS service access
  • generated from AWS service description
  • 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)

下面是一个客户端级别访问S3存储桶的对象(最多1000 **)的示例:

Here's an example of client-level access to an S3 bucket's objects (at most 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'])

**,您将必须使用 paginator 或实施您自己的循环,如果数量超过1000,则使用连续标记重复调用list_objects().

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

资源:

  • 更高级别的,面向对象的API
  • 根据资源说明生成
  • 使用标识符和属性
  • 有行动(对资源的操作)
  • 公开AWS资源的子资源和集合
  • 不提供AWS服务的100%API覆盖
  • higher-level, object-oriented API
  • generated from resource description
  • 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

以下是使用资源级别访问S3存储桶的对象(全部)的等效示例:

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)

请注意,在这种情况下,您无需进行第二次API调用即可获取对象.您可以将其作为存储桶中的集合使用.这些子资源集合是延迟加载的.

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.

您可以看到该代码的Resource版本更简单,更紧凑并且具有更多功能(它可以为您分页).如果要包括分页,则Client版本的代码实际上比上面显示的要复杂.

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.

会话:

  • 存储配置信息(主要是凭据和所选区域)
  • 允许您创建服务客户端和资源
  • boto3在需要时为您创建一个默认会话

介绍re:发明视频.

这篇关于资源,客户端和会话之间的boto3差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆