question

kiwiana avatar image
kiwiana asked

Read file from S3 and pass as a variable to ASK SDK

Hi, I have an issue with following scenario and appreciate any input/response.

The file is on S3 bucket and lambda picks up the file, reads it via AWS SDK to variable and then builds an alexa response using that variable, the issue is that because of async callback the variable in the response is always empty as response gets build before S3 callback is returned.

In older version of SDK it was handled like that:

https://github.com/alexa/alexa-cookbook/blob/master/aws/Amazon-S3/read/src/index.js

And this is what I tried for V2 SDK and it didn't work:

const TestIntent= { 
  canHandle(handlerInput) { 
    const request = handlerInput.requestEnvelope.request; 
    return request.type === 'IntentRequest' && 
    request.intent.name === 'TestIntent'
  }, 
  handle(handlerInput) {
	var myParams = { Bucket: 'mybucket', Key: 'myobject' };
	var AWS = require('aws-sdk'); var s3 = new AWS.S3();
	s3.getObject(myParams, function(err, data) { 
           if(err) { 
                 console.log(err, err.stack); }
           else { 
             var prompt = ''; 
             var fileText = data.Body.toString(); 
             prompt += fileText;
             return handlerInput.responseBuilder 
              .speak(prompt)
              .getResponse();
           } 
        }); 
  } 
};

Does anyone know what would be a right approach for this use-case?

alexa skills kitnodejssdk v2
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

roy-1 avatar image
roy-1 answered

Hi Kiwiana,

Thanks for posting. The Node SDK does not currently offer any integrated support for AWS S3. If you'd like to see this in the future, please submit a request to our uservoice page for consideration:

https://alexa.uservoice.com

As for the empty variable that you are currently returning, this can be remedied by using a promise.

You can wrap the S3 call in a promise, and resolve the response in order to return it.

I'd also recommend taking a look at our High-Low Game Sample Skill to see how something similar is accomplished using async/await for dynamodb calls:

https://github.com/alexa/skill-sample-nodejs-highlowgame/blob/master/lambda/custom/index.js

10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.