如何通过lambda函数访问bucket对象音频文件?

时间:2017-10-11 04:41:21

标签: python amazon-s3 amazon-dynamodb aws-lambda speech-recognition

我正在实现由aws s3 PUT对象创建的事件触发的aws lambda函数。这个lambda函数是一个Python语音识别器,它将从桶中获取音频上传对象(s3 PUT / upload事件)并转换将它放入文本文件,然后将数据放在dynamoDB上,同时创建SNS消息通知,以便下一个lambda函数可以检索带有相应数据的消息和id。

import speech_recognition as sr
import boto3
import os
import uuid
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
r = sr.Recognizer() 
s3 = boto3.client('s3')

def lambda_handler(event, context):

    record_id = str(uuid.uuid4())
    bucket = event['Records'][0]['s3']['bucket']['name']
    logger.info(">>>>>>>>>>>>>>>"+bucket)
    region = event['Records'][0]['awsRegion']
    logger.info(">>>>>>>>>>>>>>>"+region)
    key = event['Records'][0]['s3']['object']['key'] 
    logger.info(">>>>>>>>>>>>>>>"+key)

    # download_path = os.path.join('tmp/',key)
    audio_fl = key
    logger.info(">>>>>>>>>>>>>>>"+audio_fl)
    logger.info("start")


    with sr.AudioFile(audio_fl) as source:
        audio = r.record(source)
    try:
      text = r.recognize_google(audio)
    except sr.UnknownValueError:
      text = "UnknownValueError"
    except sr.RequestError as e:
      text = "RequestError"



    #Creating new record in DynamoDB table
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table(os.environ['DB_TABLE_NAME'])
    table.put_item(
        TextConversionProcess={
            "processID": record_id,
            "processName": "PythonSpeechRecognition",
            "processType": "SpeechRecognition",
            "processInput": audio_fl,
            "processOutput": text
            }
    )

    #Sending notification about new upload event to SNS
    client = boto3.client('sns')
    client.publish(
        TopicArn = os.environ['SNS_TOPIC'],
        Message = record_id
    )

    return record_id

我的广告素材bucket_name有一个对象object,其中包含音频文件audio.wav。但是,我收到了[Errno 2] No such file or directory: u'object/audio.wav': IOError。这个文件确实存在,但我仍然收到IO错误。有人可以帮助我吗?请注意我是aws python lambda的新手。 以下是测试事件的输出:

{
  "stackTrace": [
    [
      "/var/task/python_transcription.py",
      27,
      "lambda_handler",
      "with sr.AudioFile(audio_fl) as source:"
    ],
    [
      "/var/task/speech_recognition/__init__.py",
      203,
      "__enter__",
      "self.audio_reader = wave.open(self.filename_or_fileobject, \"rb\")"
    ],
    [
      "/usr/lib64/python2.7/wave.py",
      511,
      "open",
      "return Wave_read(f)"
    ],
    [
      "/usr/lib64/python2.7/wave.py",
      160,
      "__init__",
      "f = __builtin__.open(f, 'rb')"
    ]
  ],
  "errorType": "IOError",
  "errorMessage": "[Errno 2] No such file or directory: u'csb-motus/recordings/ceo_audio_1.wav'"
}

水桶名称:csb-motus 对象名称:recordings 对象内的文件:audio-file.wav

0 个答案:

没有答案