1 Answer
- Newest
- Most votes
- Most comments
0
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
async function runS3SelectQuery(bucketName, fileName, query) {
const params = {
Bucket: bucketName,
Key: fileName,
ExpressionType: 'SQL',
Expression: query, // Example: "SELECT * FROM S3Object s WHERE s.id = 1"
InputSerialization: {
JSON: {
Type: 'Document'
}
},
OutputSerialization: {
JSON: {}
}
};
try {
const data = await s3.selectObjectContent(params).promise();
const records = [];
// Process the result stream from S3 Select
data.Payload.on('data', (event) => {
if (event.Records) {
const recordsData = event.Records.Payload.toString();
records.push(JSON.parse(recordsData));
}
});
// Handle errors from the stream
data.Payload.on('end', () => {
console.log('Query Results:', records);
});
data.Payload.on('error', (err) => {
console.error('Error streaming S3 Select result:', err);
});
} catch (error) {
console.error('Error running S3 Select query:', error);
handleError(error);
}
}
answered a year ago
Relevant content
- asked 3 years ago
- asked 2 years ago
