我在 lambda 函数的 CSV 中获取了一些元信息/垃圾。我需要删除那些垃圾。如果我直接将文件保存到 s3,则包含垃圾。谁能指导我如何删除它?
----------------------------362648820336892682391117 ***// remove this***
Content-Disposition: form-data; name="file"; filename="Book1.csv" ***// remove this***
Content-Type: text/csv ***// remove this***
o;?name,age // remove this o;?
andy,33
hello,34
----------------------------362648820336892682391117-- ***// remove this***
我也可以使用预先签名的 URL 直接上传到 s3,但这不是我想要的。
const AWS = require('aws-sdk');
exports.handler = async (event) => {
try {
console.log(JSON.stringify(event, 2, null));
const data = new Buffer(event.body, 'base64');
const text = data.toString('ascii');
const s3 = new AWS.S3();
const params = {Bucket: 'bucket', Key: 'key', Body: text};
const d = await s3.upload(params).promise();
return {
statusCode: 200,
body: JSON.stringify('uploaded successfully'),
};
} catch (e) {
return {
statusCode: 200,
body: JSON.stringify('uploaded successfully'),
};
}
};
谢谢
答案 0 :(得分:0)
我假设您使用 multipart/form-data
上传文件。如果是这样,您将需要对请求正文进行进一步处理。您可以执行一些非常基本的操作,例如使用正则表达式手动解析内容,也可以使用 busboy 之类的库来帮助处理 HTML 表单数据。
您的场景的一个简单示例可能是这样的。
const Busboy = require('busboy');
const AWS = require('aws-sdk');
// This function uses busboy to process the event body and
// return an object containing the file data and other details.
const parseFile = (event) => new Promise((resolve, reject) => {
let contentType = event.headers['content-type']
if (!contentType) {
contentType = event.headers['Content-Type'];
}
const busboy = new Busboy({ headers: { 'content-type': contentType } });
const uploadedFile = {};
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
file.on('data', data => {
uploadedFile.data = data;
});
file.on('end', () => {
uploadedFile.filename = filename;
uploadedFile.contentType = mimetype;
});
});
busboy.on('error', error => {
reject(error);
});
busboy.on('finish', () => {
resolve(uploadedFile);
});
busboy.write(event.body, event.isBase64Encoded ? 'base64' : 'binary');
busboy.end();
});
exports.handler = async (event) => {
// Use the parse function here
const { data } = await parseFile(event);
const s3 = new AWS.S3();
const params = { Bucket: 'bucket', Key: 'key', Body: data };
await s3.upload(params).promise();
return {
statusCode: 200,
body: 'uploaded successfully',
};
};