question

Aditya.B avatar image
Aditya.B asked

Accessing a .csv file stored in AWS S3 through Lambda function (Node Js)

A .csv file is stored in AWS S3. I want to access it through Lambda function coded in NodeJs.

If there are 3 columns in .csv file, and the user gives the values of 2 attributes, then the alexa should return the value of 3rd attribute. The lambda function should search .csv file in S3 and return the required value from the row.

Please help.

A sample NodeJs code would help.

alexa skills kitlambdaaws
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

Robert G Schaffrath avatar image
Robert G Schaffrath answered

One option you can consider is the S3 selectObjectContent function. It allows you to issue simple SQL queries against a CSV (or JSON) file in S3 and return the results. In your case, you could select against criteria matching columns 1 and 2, and return the value in column 3. Something along the lines of:

select _3 from S3Object where _1 = condition1 and _2 = condition2


There is documentation at:

https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectSELECTContent.html


The Node documentation on the function is at:

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#selectObjectContent-property

1 comment
10 |5000

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

Aditya.B avatar image Aditya.B commented ·

Thank you for your Response.

I am getting an error and I cannot understand what the error is.


This is my code after adding the S3 logic :


'KpiIntent': function () {

const s3 = require('aws-sdk/clients/s3');

let filledSlots = delegateSlotCollection.call(this);

speechOutput = '';

let Machine_typeSlot = resolveCanonical(this.event.request.intent.slots.Machine_type);

console.log(Machine_typeSlot);


let dateSlot = resolveCanonical(this.event.request.intent.slots.date);

console.log(dateSlot);


let placeSlot = resolveCanonical(this.event.request.intent.slots.place);

console.log(placeSlot);


let Crew_typeSlot = resolveCanonical(this.event.request.intent.slots.Crew_type);

console.log(Crew_typeSlot);


let productSlot = resolveCanonical(this.event.request.intent.slots.product);

console.log(productSlot);


var params = {

Bucket: 'bucket_name', /* required */

Expression: 'select avg from S3Object where product=productSlot and unit=placeSlot and date=dateSlot and crew=Crew_typeSlot and machine=Machine_typeSlot',

ExpressionType: 'SQL', /* required */

InputSerialization: { /* required */

CSV: {

FieldDelimiter: ',',

FileHeaderInfo: 'USE',

RecordDelimiter: '\n'

},

},

Key: 'key', /* required */

OutputSerialization: { /* required */

CSV: {

},

},

};

s3.selectObjectContent(params, function(err, data) {

if (err) console.log(err, err.stack); // an error occurred

else {console.log(data);

speechOutput=data;

}// successful response

});

//speechOutput = "KPI INTENT";

this.emit(':ask', speechOutput, speechOutput);

},

0 Likes 0 ·