AWS Lambda中的Boto3 S3列表对象抛出错误

moh*_*han 2 python amazon-s3 amazon-web-services boto3 aws-lambda

lambda中的代码:

import boto3

def lambda_handler(event, context):
    s3_client = boto3.resource('s3')
    mybucket = s3_client.Bucket('bucket-name')
    for object in mybucket.objects.all():
       print(object)

    for key in s3_client.list_objects(Bucket='bucket-name')['Contents']:
       print(key['Key'])'
Run Code Online (Sandbox Code Playgroud)

第一个“ for”块列出存储桶中的所有键,但是第二个“ for”块引发以下错误。

's3.Service Resource' object has no attribute 'list_objects' : AttributeError
Run Code Online (Sandbox Code Playgroud)

根据http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.list_buckets,这没有任何意义。关于可能是什么问题的任何提示?我使用了python 2.7和python 3.6

hjp*_*r92 6

一个boto3服务资源是不一样的旧博托图书馆的服务客户。显然您正在混合两者的文档。

客户端是低级客户端,只是将AWS API包装为python基本数据类型。所有服务在boto3中都有一个可用的客户端。

请检查有关资源客户端的文档。

资源资源

资源表示与Amazon Web Services(AWS)的面向对象的接口。与服务客户端进行的原始低级调用相比,它们提供了更高级别的抽象。

resource = boto3.resource('s3')
Run Code Online (Sandbox Code Playgroud)

客户群

客户端为AWS提供了一个低级接口,其方法与服务API的映射接近1:1。客户端支持所有服务操作。客户端是从JSON服务定义文件生成的。

client = boto3.client('s3')
Run Code Online (Sandbox Code Playgroud)