gpt4 book ai didi

python - AWS lambda 函数错误

转载 作者:行者123 更新时间:2023-12-04 17:50:23 26 4
gpt4 key购买 nike

我已经创建了一个带有 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 中有 2 个主要文件。第一个文件包含主要功能,另一个文件具有安全凭证,另一个文件夹和文件是 lib 的。名为 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 中创建一个文件夹。所以也许谁知道我做错了什么?请帮忙。

最佳答案

例如,AWS Lambda 处理程序有两个参数事件和上下文:

def lambda_handler(event, context):

关于python - AWS lambda 函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45480644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com