何时使用 boto3 客户端以及何时使用 boto3 资源?

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

我想了解什么时候应该使用 Resource and when I should use a Client

boto3 文档中提供的定义并未明确说明何时更适合使用其中一种。

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

它的意思是将连接的资源附加到您以后可以在不指定原始资源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 是低级别的,您没有 "entry-class object",因此您必须为您执行的每个操作明确指定它连接到的确切资源。

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