问题描述
我试图将csv从S3上载到Lambda中以进行简单处理,但是却收到了““ errorMessage”:“'模块'对象不可调用”,“。
import boto3
s3_client = boto3("s3")
def readindata(event,context):
bucket_name = event['Records'][0]['s3']['bucket']['name']
s3_file_name = event['Records'][0]['s3']['object']['key']
resp = s3_client.get_object(Bucket=bucket_name,Key=s3_file_name)
data = resp['Body'].read().decode("utf-8")
print(data)
我为此使用了无服务器和CloudFormation,但是Yaml文件也非常简单:
service: readincsvdata2
# app and org for use with dashboard.serverless.com
#app: your-app-name
#org: your-org-name
# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
# frameworkVersion: "=X.X.X"
provider:
name: aws
runtime: python3.8
region: us-east-1
profile: serverless-admin
timeout: 10
memorySize: 128
iamRoleStatements:
- Effect: "Allow"
Action:
- "s3:*"
Resource: "*"
custom:
bucket: prototypeuploadcsv-08-13-2020v2
pythonRequirements:
dockerizePip: true
functions:
protomodel-readcsv:
handler: handler.readindata
events:
- s3:
bucket: ${self:custom.bucket}
event: s3:ObjectCreated:*
而且我不断收到此错误:
Response:
{
"errorMessage": "'module' object is not callable","errorType": "TypeError","stackTrace": [
" File \"/var/lang/lib/python3.8/imp.py\",line 234,in load_module\n return load_source(name,filename,file)\n"," File \"/var/lang/lib/python3.8/imp.py\",line 171,in load_source\n module = _load(spec)\n"," File \"<frozen importlib._bootstrap>\",line 702,in _load\n",line 671,in _load_unlocked\n"," File \"<frozen importlib._bootstrap_external>\",line 783,in exec_module\n",line 219,in _call_with_frames_removed\n"," File \"/var/task/handler.py\",line 3,in <module>\n s3_client = boto3(\"s3\")\n"
]
}
Request ID:
似乎我在调用或实例化一个对象错误,但是我没有看到它。我只是使用一个超级简单的测试csv作为测试数据。不确定需要更改什么。
解决方法
可能是由于以下错字造成的。
s3_client = boto3("s3")
应该是
s3_client = boto3.client("s3")
,
代替:
s3_client = boto3("s3")
应该是:
s3_client = boto3.client("s3")