我正在嘗試使用以下代碼創建一個 lambda 函式,當我嘗試部署和測驗該函式時,出現錯誤。我需要該函式的想法是 lambda 應該監視 s3 中的檔案夾,以查看物件是否在其中超過 1 小時并通知我。
import boto3
from datetime import datetime, timedelta
from dateutil.tz import tzutc, UTC
BUCKET = 'my-bucket'
TOPIC_ARN = 'arn:aws:sns:ap-southeast-2:123456789012:Old-File-Warning'
s3_resource = boto3.resource('s3')
sns_resource = boto3.resource('sns')
sns_topic = sns_resource.Topic(TOPIC_ARN)
for object in s3_resource.Bucket(BUCKET).objects.filter(Prefix='folder1/folder2/'):
if object.last_modified > datetime.now(tzutc()) - timedelta(hours = 1):
message = f"Object {object.key} is more than 1 hour old!"
sns_topic.publish(Message=message)
我收到此錯誤 { "errorMessage": "Handler 'lambda_handler' missing on module 'lambda_function'", "errorType": "Runtime.HandlerNotFound", "stackTrace": [] }
當我在下面嘗試時
def handler_name(event, context):
s3_resource = boto3.resource('s3')
sns_resource = boto3.resource('sns')
sns_topic = sns_resource.Topic(TOPIC_ARN)
return
s3_resource
sns_topic
sns_resource
我收到 [ERROR] NameError: name 's3_resource' 未定義
我究竟做錯了什么?
uj5u.com熱心網友回復:
默認名稱是lambda_handler,不是handler_name。因此,要解決您的錯誤,它應該是:
def lambda_handler(event, context):
s3_resource = boto3.resource('s3')
sns_resource = boto3.resource('sns')
sns_topic = sns_resource.Topic(TOPIC_ARN)
return
請注意,您的代碼還有很多問題。答案僅解決您報告的錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/330991.html
