【问题标题】:Boto3 S3 list object throwing error in AWS lambdaAWS lambda中的Boto3 S3列表对象抛出错误
【发布时间】:2017-05-12 06:31:54
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: python amazon-web-services amazon-s3 aws-lambda boto3


    【解决方案1】:

    boto3 service resource 与旧版 boto 库的服务客户端不同。您显然是在混合两者的文档。

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

    请查看a Resourcea Client 的文档。

    资源

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

    resource = boto3.resource('s3')
    

    客户

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

    client = boto3.client('s3')
    

    【讨论】:

      猜你喜欢
      • 2019-01-24
      • 2019-08-03
      • 2020-01-22
      • 1970-01-01
      • 2020-07-04
      • 1970-01-01
      • 2020-06-21
      • 2019-11-17
      • 2015-08-23
      相关资源
      最近更新 更多