繁体   English   中英

AWS Lambda Python S3读取文件错误

[英]AWS Lambda Python S3 Read File Error

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

任何帮助将非常感激

此错误是由创建boto3 客户端而不是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)直接使用“ 低级 ” s3客户端download_file()

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

与Boto3低级客户端合作

我认为可能是变量s3对象类型错误引用。 s3ResourceMeta对象时,可能会使用s3.meta.client ,但是在这里我认为s3Client对象。

所以你可以写

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

暂无
暂无

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

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