簡體   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