简体   繁体   English

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

[英]Boto3 S3 list object throwing error in AWS lambda

Code in 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'])'

the first 'for' block list all the keys in the bucket but the second 'for' block throws following error. 第一个“ for”块列出存储桶中的所有键,但是第二个“ for”块引发以下错误。

's3.Service Resource' object has no attribute 'list_objects' : AttributeError

It doesn't make sense based on http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.list_buckets . 根据http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.list_buckets,这没有任何意义。 any hint on what could be the problem? 关于可能是什么问题的任何提示? i used python 2.7 as well as python 3.6 我使用了python 2.7和python 3.6

A boto3 service resource is not the same as the older boto library's service client. boto3服务资源与旧的boto库的服务客户端不同。 You are apparently mixing the documentation of the two. 显然您正在混合两者的文档。

A client is low level client, and just wraps the AWS APIs to python basic datatypes. 客户端是低级客户端,只是将AWS API包装为python基本数据类型。 All services have a client available in boto3. 所有服务在boto3中都有一个可用的客户端。

Do check the documentation for a Resource and a Client . 请检查有关资源客户端的文档。

Resources 资源

Resources represent an object-oriented interface to Amazon Web Services (AWS). 资源表示与Amazon Web Services(AWS)的面向对象的接口。 They provide a higher-level abstraction than the raw, low-level calls made by service clients. 与服务客户端进行的原始低级调用相比,它们提供了更高级别的抽象。

 resource = boto3.resource('s3') 

Clients 客户端

Clients provide a low-level interface to AWS whose methods map close to 1:1 with service APIs. 客户端为AWS提供了一个低级接口,其方法与服务API的映射接近1:1。 All service operations are supported by clients. 客户端支持所有服务操作。 Clients are generated from a JSON service definition file. 客户端是从JSON服务定义文件生成的。

 client = boto3.client('s3') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM