代码之家  ›  专栏  ›  技术社区  ›  JLuu

将CSV从S3上传到AWS Lambda时出错:TypeError:“模块”对象不可调用

  •  0
  • JLuu  · 技术社区  · 5 年前

    我试图将一个csv从S3上传到Lambda进行简单处理,但收到了“errorMessage”:“'module'对象不可调用”。

    我使用的是Python3.8,这是一个简单的Lambda函数:

    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",
        "  File \"<frozen importlib._bootstrap>\", line 671, in _load_unlocked\n",
        "  File \"<frozen importlib._bootstrap_external>\", line 783, in exec_module\n",
        "  File \"<frozen importlib._bootstrap>\", 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作为测试数据。不确定需要改变什么。

    2 回复  |  直到 5 年前
        1
  •  2
  •   jellycsc    5 年前

    可能是由于以下拼写错误。

    s3_client = boto3("s3")
    

    应该是

    s3_client = boto3.client("s3")
    
        2
  •  1
  •   Marcin    5 年前

    而不是:

    s3_client = boto3("s3")
    

    应该有:

    s3_client = boto3.client("s3")