AWS Lambda函数错误

3

我使用Python函数创建了一个部署包,用于使用AWS Lambda创建Google Drive文件夹。然后我尝试测试它,但是出现了错误:

    {
  "errorMessage": "main() takes from 0 to 1 positional arguments but 2 were given",
  "errorType": "TypeError",
  "stackTrace": [
    [
      "/var/runtime/awslambda/bootstrap.py",
      249,
      "handle_event_request",
      "result = request_handler(json_input, context)"
    ]
  ]
}

我有一个.zip文件,其中包含两个主要文件。第一个文件包含主要功能,另一个文件包含安全凭据,其他文件和文件夹是库。主文件名为lambda_function.py,包含以下代码:

from __future__ import print_function

import httplib2
import os

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

try:
    import argparse

    flags=argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags=None

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/drive-python-quickstart.json
SCOPES='https://www.googleapis.com/auth/drive.file'
CLIENT_SECRET_FILE='client_secret.json'
APPLICATION_NAME='Drive API Python Quickstart'


def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir=os.path.expanduser('~')
    credential_dir=os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path=os.path.join(credential_dir,
                                 'drive-python-quickstart.json')

    store=Storage(credential_path)
    credentials=store.get()
    if not credentials or credentials.invalid:
        flow=client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent=APPLICATION_NAME
        if flags:
            credentials=tools.run_flow(flow, store, flags)
        print('Storing credentials to ' + credential_path)
    return credentials

def main(drive_service=None):
    """Shows basic usage of the Google Drive API.

    Creates a Google Drive API service object and outputs the names and IDs
    for up to 10 files.
    """
    credentials=get_credentials()
    http=credentials.authorize(httplib2.Http())
    service=discovery.build('drive', 'v3', http=http)

    file_metadata={
        'name': 'Invoices',
        'mimeType': 'application/vnd.google-apps.folder'
    }
    file=service.files().create(body=file_metadata,
                                      fields='id').execute()
    print('Folder ID: %s' % file.get('id'))

if __name__ == '__main__':
    main()

我的 AWS Lambda 处理器为 lambda_function.main,如果我尝试进行测试,就会出现错误。但如果我在控制台中执行此代码并在 Google Drive API 中创建一个文件夹,则可以成功运行。也许有人知道我做错了什么?请帮忙。

1个回答

1
AWS Lambda处理程序有两个参数:事件和上下文,例如:
def lambda_handler(event, context):

谢谢回复,看一下,现在我修复了,但是我得到了下一个错误,"errorMessage": "模块初始化错误",并且在日志中它看起来像:module initialization error: [Errno 30] 只读文件系统:'/home/sbx_user1078'。 - Андрей Ка
你需要创建一个包含你所使用的库的部署包,参考:http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html - drsromero
我已经创建好了!所有模块都在.zip文件中。我使用virualenv创建它,步骤如下:
  1. virtualenv -p /usr/bin/python3.5 google
  2. 使用以下命令安装Google Drive API模块:pip install --upgrade google-api-python-client
  3. 将site-packages中的所有模块添加到zip文件中。
- Андрей Ка
错误:[Errno 30] 只读文件系统: 我不确定,但可能是与tmp路径有关的问题,请在此处阅读:https://dev59.com/E1kS5IYBdhLWcg3w25sM - drsromero

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接