何时使用boto3客户端以及何时使用boto3资源? [英] 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.
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屋!