Created
May 1, 2019 17:41
-
-
Save dhavaln/516442135814b529a707440cb4bde04a to your computer and use it in GitHub Desktop.
S3 Select query from NodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const | |
| AWS = require('aws-sdk'), | |
| { Readable } = require('readable-stream'); | |
| const s3 = new AWS.S3({ | |
| region: 'ap-northeast-2' | |
| }); | |
| s3SelectQuery = (query) => { | |
| return new Promise((resolve, reject) => { | |
| var params = { | |
| Bucket: process.env.BUCKET_NAME, | |
| Key: process.env.FILE_NAME, | |
| Expression: query, | |
| ExpressionType: 'SQL', | |
| InputSerialization: { | |
| CSV: { | |
| FieldDelimiter: ',', | |
| FileHeaderInfo: 'USE', | |
| RecordDelimiter: '\n' | |
| }, | |
| CompressionType: 'NONE' | |
| }, | |
| OutputSerialization: { | |
| CSV: { | |
| FieldDelimiter: ',', | |
| RecordDelimiter: '\n' | |
| } | |
| } | |
| }; | |
| let resultData = ""; | |
| s3.selectObjectContent(params, function(err, data) { | |
| if(!err){ | |
| data.Payload.on('data', (data) => { | |
| if(data.Records && data.Records.Payload){ | |
| let str = Buffer.from(data.Records.Payload); | |
| resultData += str; | |
| } | |
| }); | |
| data.Payload.on('end', (data) => { | |
| resolve(resultData); | |
| }) | |
| }else{ | |
| reject(err); | |
| } | |
| }); | |
| }); | |
| } | |
| s3SelectQuery( | |
| 'Select s.InvoiceID, s.ProductCode, s.UsageQuantity, s.CurrencyCode, s.TotalCost from s3Object s limit 5' | |
| ).then( data => { | |
| console.log(data); | |
| }) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment