【问题标题】:AWS Lambda Python S3 Read File ErrorAWS Lambda Python S3 读取文件错误
【发布时间】:2017-11-19 11:03:50
【问题描述】:

尝试从 s3 的存储桶中读取文件。存储桶有一个连接到 python lambda 函数的触发器。然后在将新文件放入存储桶时运行。我不断收到错误消息。

这是代码:

将文件从 S3 下载到本地文件系统

try:
    s3.meta.client.download_file(bucket, key, localFilename)
except Exception as e:
    print(e)
    print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
    raise e

我收到此错误

'ClientMeta' 对象没有属性'client'

我认为它可能是 IAM,但 Lambda 函数具有 AWSLambdaFullAccess 角色,这几乎是 S3 中的管理员。

任何帮助将不胜感激

【问题讨论】:

  • 在我看来,s3.meta 好像没有 client 对象,所以上面的调用甚至在到达 AWS 之前就失败了。我猜这个例外是AttributeError?请编辑您的问题,以包括变量 s3 从何处获取其值。还请包括完整的回溯。
  • 为什么不用boto3 s3低级客户端下载文件。 import boto3 # Get the service client s3 = boto3.client('s3') # Download object at bucket-name with key-name to tmp.txt s3.download_file("bucket-name", "key-name", "tmp.txt").

标签: python amazon-web-services amazon-s3 lambda amazon-iam


【解决方案1】:

此错误是由创建 boto3 client 而不是 resource 引起的。

示例(这将重现您的错误):

s3 = boto3.client('s3')
s3.meta.client.download_file(bucket, key, localFilename)

您有两种解决方案:

1) 更改为使用“高级抽象”s3资源:

s3 = boto3.resource('s3')
s3.meta.client.download_file(bucket, key, localFilename)

2) 直接使用“low-level”s3客户端download_file()

s3 = boto3.client('s3')
s3.download_file(bucket, key, localFilename)

Working with Boto3 Low-Level Clients

【讨论】:

  • 修复了它。谢谢约翰。
【解决方案2】:

我认为变量s3 的对象类型可能不正确。 s3.meta.client 可能是您在 s3ResourceMeta 对象时使用的东西,但在这里我认为 s3Client 对象。

所以你可以写

try: s3.download_file(bucket, key, localFilename) except Exception as e: ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 2019-10-15
    • 2017-05-11
    • 2017-01-15
    相关资源
    最近更新 更多