我有一个Lambda函数,它从S3导入特定的CSV文件到MySQL。然而,CSV文件的大小约为1 GB。当我运行此代码时,它不能处理并超时。
//s3 to rds
const fs = require("fs");
const AWS = require('aws-sdk');
var mysql = require('mysql');
var config = require('./config.json');
const s3 = new AWS.S3({
accessKeyId: 'XXXXXXXXXXXXXXX',
secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXxx'
});
var filePath = `localfilepath`;
var pool = mysql.createPool({
host: config.dbhost,
user: config.dbuser,
password: config.dbpassword,
database: config.dbname
});
pool.getConnection((err, connection) => {
if (err) throw err;
console.log("Connected!" + connection);
var s3Params = {
Bucket: '<your_bucket_name>',
Key: '<your_key>'
};
s3.getObject(s3Params, function(err, result) {
if (err) {
throw new Error(err);
} else {
console.log('file stored successfully', result);
fs.createWriteStream(filePath).write(result.Body);
connection.query('TRUNCATE TABLE <table_name>', (err, result) => {
if (err) {
throw new Error(err);
} else {
console.log('table truncated');
var query = `LOAD DATA LOCAL INFILE '<file_name>' INTO table <table_name> FIELDS TERMINATED BY ',' ENCLOSED BY '"' IGNORE 1 LINES `;
connection.query(query, function(err, result) {
if (err) throw err;
console.log("Result: " + result);
connection.release();
fs.unlinkSync(filePath);
console.log('file deleted');
});
}
});
}
});
})
我该如何让它运行起来?