何时使用boto3客户端以及何时使用boto3资源? [英] When to use a boto3 client and when to use a boto3 resource?

查看:130
本文介绍了何时使用boto3客户端以及何时使用boto3资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解何时应该使用资源和当我应该使用客户端.

I am trying to understand when I should use a Resource and when I should use a Client.

boto3文档中提供的定义并没有明确表明何时最好使用其中一个.

The definitions provided in boto3 docs don't really make it clear when it is preferable to use one or the other.

推荐答案

boto3.resource是环绕boto3.client的高级服务类.

boto3.resource is a high-level services class wrap around boto3.client.

这是为了将连接的资源附加在下面,您以后可以在不指定原始资源ID的情况下使用其他资源.

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是低级的,您没有入口类对象",因此您必须为执行的每个操作显式指定其连接的确切资源.

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.

这取决于个人需求.但是,boto3.resource不能包装所有的boto3.client功能,因此有时您需要调用boto3.client或使用boto3.resource.meta.client来完成工作.

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.

这篇关于何时使用boto3客户端以及何时使用boto3资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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