31 lines
951 B
JavaScript
31 lines
951 B
JavaScript
// s3.js
|
|
require('dotenv').config();
|
|
const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3');
|
|
const { stringify } = require('csv-stringify/sync');
|
|
|
|
const s3 = new S3Client({
|
|
region: process.env.AWS_REGION
|
|
});
|
|
|
|
/**
|
|
* Uploads a CSV to s3://<bucket>/trends/<key>.csv with public-read ACL.
|
|
* @param {string} key Date key like '20250423'
|
|
* @param {Array<Object>} rows Array of DB rows to stringify into CSV
|
|
* @returns {string} Public URL of the uploaded CSV
|
|
*/
|
|
async function uploadTrendsCsv(key, rows) {
|
|
// Convert rows to CSV string (includes header)
|
|
const csv = stringify(rows, { header: true });
|
|
const cmd = new PutObjectCommand({
|
|
Bucket: process.env.S3_BUCKET_NAME,
|
|
Key: `trends/${key}.csv`,
|
|
Body: csv,
|
|
ContentType: 'text/csv',
|
|
ACL: 'public-read'
|
|
});
|
|
await s3.send(cmd);
|
|
return `${process.env.S3_BASE_URL}/${key}.csv`;
|
|
}
|
|
|
|
module.exports = { uploadTrendsCsv };
|