Saturday, 29 April 2017

How to put object from aws lambda to S3 with Node.js

I am trying to put a audio file to S3 via Polly with lambda(Node.js).

Lambda log shows params.Body to be a string, Buffer, Stream, Blob, or typed array object’.
I think I have to use stream API, but I could not fix it.

Polly.synthesizeSpeech-property is here
S3.putObject-property is here

var AWS = require('aws-sdk');
var polly = new AWS.Polly();
var s3 = new AWS.S3();

function create_voice(){
  var params = {
    OutputFormat: "mp3", 
    SampleRate: "16000", 
    Text: “this is test”, 
    TextType: "text", 
    VoiceId: "Mizuki"
  };

  polly.synthesizeSpeech(params, function(err, data) {
    if (err) {
        console.log("polly error: ",err, err.stack); 
    } else {  
        var s3params = {
            Bucket: "allys-voice/weather",
            Key: “test.mp3",
            Body: data 
       /*
       data = {
        AudioStream: <Binary String>, 
        ContentType: "audio/mpeg", 
        RequestCharacters: 37
       }
       */
        };
        s3.putObject(s3params, function(err, data) {
            if (err) {
                console.log("s3 error: ", err, err.stack);
            }else{     
                console.log("uploaded to S3!");
            }
       });
    }
  });
}

exports.handler = (event, context, callback) => {
    create_voice();
    callback(null, 'Hello from Lambda');
};



via marceline

No comments:

Post a Comment