简体   繁体   中英

Trying to send readable stream (audio) to amazon s3 - runtime error

I am trying to write a function that takes the mp3 url of the recording and then uploads that to S3. However, I keep getting a runtime error and the callback is never reached. If I move the callback below s3.upload(...) then the statement "attempting to upload mp3 is never logged.

exports.handler = function(context, event, callback) {
  const twiml = new Twilio.twiml.VoiceResponse();
  var AWS = require('aws-sdk');
  var s3 = new AWS.S3();
  var getUri = require('get-uri');

  AWS.config.update({
    accessKeyId: "...",
    secretAccessKey: "..."
  });

  var client = context.getTwilioClient();
  const recording_id = event.RecordingSid;
  const uri = event.RecordingUrl + ".mp3";

  getUri(uri, function (err, rs) {
    if (err) {
        console.log(err.message);
        throw err;
    }
    var params = {
        ACL: "public-read",
        Body: rs,
        Bucket: "...",
        Key: "audio.mp3",
        ContentType: 'audio/mp3'
    };
    s3.upload(params, function(err,data) {
        console.log("attempting to upload mp3");
        if (err) {
            console.log("there is an error");
            console.log(err.status);
            throw err.message;
        }
        else {
            console.log("Your upload has been successful.");
          }
        callback(null, twiml);
      });
});


   console.log("at the end");
 };

Is there any other way to access the recording and put them in my public s3 bucket? Why is this never executing s3.upload(...) .
Any insights into this is helpful! Thanks in advance!

   app.get('/uploadsong',function(req,res){
    console.log("Hi there")
    var URI = 'http://sensongsmp3download.info/Kaala%20(2018)%20-%20Sensongsmp3.info/Thanga%20Sela%20--%20Sensongsmp3.Info.mp3';
    var buffer = [];

    request
        .get(URI)
        .on('error', function(err) {
        console.log("error")
    }).on('data',function(data){
        buffer.push(data);
    }).on('end',function(){
        var completeSong = Buffer.concat(buffer);

        var data = {
            Body:completeSong,
            Key: 'sample.mp3',
            ContentType: 'audio/mp3'
        }

        s3Bucket.putObject(data, function(err, data){ 
            if (err) 
            { 
                console.log('Error uploading data: ', data); 
            } else 
            {
                console.log('upload successfull')
                res.send('done');
            }

        })



    })

})

here are the modules i have used

var request = require('request');

I contacted Twilio regarding this they responded that Twilio Functions have a strict 5 second time-out and the upload from the Twilio Function to S3 Bucket takes more than 5 seconds. My workaround was sending a string with all mp3 URLs separated by comma and a space. The lambda function would then parse through all the links and store all links in an array which would be used for audio playback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM