2

AWSラムダでgoogleドライブフォルダを作成するためのPython関数を使用してデプロイメントパッケージを作成しました。そして、私はそれをテストしようと、私はエラーを取得しました:AWSラムダ関数エラー

{ 
    "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_function.mainされます。私はコンソールでそれを行う場合、私は正常にこのコードを実行し、GoogleドライブのAPIにフォルダを作成します。だから誰が私が間違っていることを知っている?助けてください。

答えて

0

AWSラムダハンドラは、例えば二つの引数イベントおよびコンテキストがあります:返信用

def lambda_handler(event, context): 
+0

おかげで、見て、今私はそれを修正するが、私は次のエラーを取得し、「にErrorMessage」:「モジュールの初期化エラー」とモジュールの初期化エラー:[Errno 30]読み取り専用ファイルシステム: '/ home/sbx_user1078' – Andrej

+0

使用しているライブラリでデプロイメントパッケージを作成する必要があります。http://docs.aws .amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html – drsromero

+0

すでに作成済みです!すべてのモジュールは.zipファイルにあります。私はvirualenvを使用してそれを作成します。手順は次のとおりです。 1. virtualenv -p /usr/bin/python3.5 google 2. Googleドライブのapiモジュールをインストールします。pip install -upgrade google-api-python-client 3. addすべてのモジュールをサイトパッケージからファイルに圧縮する – Andrej