“名称‘boto3’未定义”

问题描述 投票:0回答:1

我正在尝试创建基本的 Lambda 函数来连接到 S3,然后执行其余的操作,但我在开始连接到 S3 时陷入困境,并在运行代码时遇到以下错误。

import json
import boto3
import io
from io import StringIO
import pandas as pd

s3_client = boto3.client('s3')

def lambda_handler(event, context):
    try:
        s3_Bucket_Name = "mybucket"
        s3_File_Name = "toconvert/abcd.xlsx" 
                
    except Exception as err:
        print(err)
        
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

错误:

Response
{
  "errorMessage": "name 'boto3' is not defined",
  "errorType": "NameError",
  "requestId": "<RequestId>",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 5, in lambda_handler\n    s3 = boto3.client('s3')\n"
  ]
}

Function Logs
START RequestId: <RequestId> Version: $LATEST
[ERROR] NameError: name 'boto3' is not defined
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 5, in lambda_handler
    s3 = boto3.client('s3')END RequestId: <RequestId>
REPORT RequestId: <RequestId>   Duration: 13.35 ms  Billed Duration: 14 ms  Memory Size: 128 MB Max Memory Used: 33 MB  Init Duration: 126.05 ms
aws-lambda boto3
1个回答
0
投票

我认为你的代码片段和回溯不适合放在一起。请注意,它显示

 line 5, in lambda_handler\n    s3 = boto3.client('s3')
但您的代码显示了在“lambda_function.py”中的 init 期间函数定义之前客户端的初始化。

事实上,既然你导入了 boto3,它就必须存在于命名空间和 lambda 层中。否则你已经收到了

ImportError: No module named boto3

© www.soinside.com 2019 - 2024. All rights reserved.