AWS Lambda Python S3读取文件错误

时间:2017-11-19 11:03:51

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

尝试在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中的管理员。

非常感谢任何帮助

2 个答案:

答案 0 :(得分:3)

此错误是由创建boto3 客户端而非资源引起的。

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

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)直接使用" 低级" s3客户端download_file()

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

Working with Boto3 Low-Level Clients

答案 1 :(得分:0)

我认为可能会为变量s3引用错误的对象类型。当s3.meta.clients3个对象时,您可能会使用ResourceMeta,但我认为s3Client个对象。

所以你可以写

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