Alexa Skill для загрузки файла с Google Диска

Я пытаюсь загрузить файл с диска Google, используя Alexa Skill и Amazon Echo, который я сделал с малиной Pi 3. Моя лямбда-функция выглядит следующим образом: -

#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
import xml.etree.ElementTree as etree
from datetime import datetime as dt
import os
import urllib
import requests

from urllib.parse import urlparse


def lambda_handler(event, context):
    """ Route the incoming request based on type (LaunchRequest, IntentRequest,
    etc.) The JSON body of the request is provided in the event parameter.
    """

    print('event.session.application.applicationId=' + event['session'
          ]['application']['applicationId'])

    # if (event['session']['application']['applicationId'] !=
    #         "amzn1.echo-sdk-ams.app.[unique-value-here]"):
    #     raise ValueError("Invalid Application ID")

    if event['session']['new']:
        on_session_started({'requestId': event['request']['requestId'
                           ]}, event['session'])

    if event['request']['type'] == 'LaunchRequest':
        return on_launch(event['request'], event['session'])
    elif event['request']['type'] == 'IntentRequest':
        return on_intent(event['request'], event['session'])
    elif event['request']['type'] == 'SessionEndedRequest':
        return on_session_ended(event['request'], event['session'])


def on_session_started(session_started_request, session):
    """ Called when the session starts """

    print('on_session_started requestId='
          + session_started_request['requestId'] + ', sessionId='
          + session['sessionId'])


def on_launch(launch_request, session):
    """ Called when the user launches the skill without specifying what they
    want
    """

    print('on_launch requestId=' + launch_request['requestId']
          + ', sessionId=' + session['sessionId'])

    # Dispatch to your skill's launch

    return get_welcome_response()


def on_intent(intent_request, session):
    """ Called when the user specifies an intent for this skill """

    print('on_intent requestId=' + intent_request['requestId']
          + ', sessionId=' + session['sessionId'])

    intent = intent_request['intent']
    intent_name = intent_request['intent']['name']

    # Dispatch to your skill's intent handlers

    if intent_name == 'DownloadFiles':
        return get_file(intent, session)
    elif intent_name == 'AMAZON.HelpIntent':
        return get_welcome_response()
    else:
        raise ValueError('Invalid intent')


def on_session_ended(session_ended_request, session):
    """ Called when the user ends the session.Is not called when the skill returns should_end_session=true """

    print('on_session_ended requestId='
          + session_ended_request['requestId'] + ', sessionId='
          + session['sessionId'])


    # add cleanup logic here

# --------------- Functions that control the skill's behavior ------------------

def get_welcome_response():
    """ If we wanted to initialize the session to have some attributes we could add those here """

    session_attributes = {}
    card_title = 'Welcome'
    speech_output = \
        "Welcome to file download Application. Please ask me to download files by saying, Ask auto downloader for download"

    # If the user either does not reply to the welcome message or says something
    # that is not understood, they will be prompted again with this text.

    reprompt_text = \
        "Please ask me to download files by saying, Ask auto downloader for download"
    should_end_session = False
    return build_response(session_attributes,
                          build_speechlet_response(card_title,
                          speech_output, reprompt_text,
                          should_end_session))


def get_file(intent, session):
    """ Grabs the files from the path that have to be downloaded """

    card_title = intent['name']
    session_attributes = {}
    should_end_session = True
    username = '*******'
    password = '*******'

    url = 'https://drive.google.com/drive/my-drive/abc.pdf'
    filename = os.path.basename(urlparse(url).path)

    # urllib.urlretrieve(url, "code.zip")

    r = requests.get(url, auth=(username, password))

    if r.status_code == 200:
        with open(filename, 'wb') as out:
            for bits in r.iter_content():
                out.write(bits)

    speech_output = 'The file filename has been downloaded'
    return build_response(session_attributes,
                          build_speechlet_response(card_title,
                          speech_output, reprompt_text,
                          should_end_session))


# --------------- Helpers that build all of the responses ----------------------

def build_speechlet_response(
    title,
    output,
    reprompt_text,
    should_end_session,
    ):
    return {
        'outputSpeech': {'type': 'PlainText', 'text': output},
        'card': {'type': 'Simple', 'title': 'SessionSpeechlet - ' \
                 + title, 'content': 'SessionSpeechlet - ' + output},
        'reprompt': {'outputSpeech': {'type': 'PlainText',
                     'text': reprompt_text}},
        'shouldEndSession': should_end_session,
        }


def build_response(session_attributes, speechlet_response):
    return {'version': '1.0', 'sessionAttributes': session_attributes,
            'response': speechlet_response}

После запуска этой функции lambda_function я получаю следующую ошибку: -

[Errno 30] Read-only file system: 'abc.pdf': OSError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 32, in lambda_handler
return on_intent(event['request'], event['session'])
File "/var/task/lambda_function.py", line 70, in on_intent
return get_file(intent, session)
File "/var/task/lambda_function.py", line 126, in get_file
with open(filename, 'wb') as out:
OSError: [Errno 30] Read-only file system: 'abc.pdf'

Кто-нибудь может подсказать, как справиться с этой ошибкой! Я хочу, чтобы Алексе было предложено загрузить файлы с фиксированного URL-адреса с помощью amazon echo.


person Seeker90    schedule 31.01.2018    source источник


Ответы (1)


Обязательно записывайте в /tmp, так как это единственный доступный для записи путь в лямбда-выражении.

with open("/tmp/" + filename, 'wb') as out:
            for bits in r.iter_content():
                out.write(bits)
person Pandelis    schedule 31.01.2018
comment
Привет, Панделис. Спасибо! Это решило ошибку. Файл загружается в каталог /tmp, верно? Как я могу получить доступ к файлу в каталоге /tmp? Есть ли способ вместо этого загрузить файл в мою локальную системную папку? - person Seeker90; 01.02.2018
comment
Нет, лямбда работает в облаке, а не на вашем компьютере - person Pandelis; 01.02.2018
comment
В порядке. Как мне тогда получить доступ к файлам из каталога /tmp? извините, я новичок в AWS и Lambda. - person Seeker90; 01.02.2018
comment
Вам придется загрузить его куда-нибудь, к которому у вас есть доступ. S3 Возможно? - person Pandelis; 05.02.2018
comment
Как загрузить файл и загрузить его на S3 в том же коде? Не могли бы вы помочь @pandelis? - person Seeker90; 08.02.2018