3

I've created a deployment package with python function to create a google drive folder with AWS Lambda. Then I try to test it and I've get an error :

    {
  "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)"
    ]
  ]
}

I've 2 main files in my .zip. First file are contain main function and another file have security credents , another folders and files are lib's. main file named lambda_function.py with code:

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()

and my handler in AWS Lambda is lambda_function.main , if I try to test I get an error. If I do it at the console I successfully execute this code and create a folder in google drive api. So maybe whom know what I do wrong ? Help please.

1 Answer 1

1

The AWS Lambda handler has two arguments event and context for example:

def lambda_handler(event, context):
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for reply , look , now i fix it but i get the next error , "errorMessage": "module initialization error" and in log it looks like : module initialization error: [Errno 30] Read-only file system: '/home/sbx_user1078'
You need to create a deployment package with the libs you are using, read: docs.aws.amazon.com/lambda/latest/dg/…
i already created it ! all modules are in .zip file. i create it using virualenv , steps are : 1. virtualenv -p /usr/bin/python3.5 google 2. install google drive api modules with : pip install --upgrade google-api-python-client 3. add to zip file all modules from site-packages
The error: [Errno 30] Read-only file system: I'm not sure but it could be a problem with the tmp path, read here: stackoverflow.com/questions/39383465/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.