Ank*_*yal 3 csv amazon-s3 amazon-web-services node.js
我正在从 JSON 内容动态创建一个 CSV 文件,并在 S3 存储桶上上传生成的 CSV 文件,而不是首先将文件保存在本地。
下面是我的代码片段,因为使用下面的代码将我的 CSV 文件上传到 S3 存储桶,但它似乎不是正确的 CSV 格式。
var uploadCSVFileOnS3Bucket = function(next, csvFileContent,results) {
console.log("uploadCSVFileOnS3Bucket function started");
var bufferObject = new Buffer.from(JSON.stringify(csvFileContent));
var filePath = configurationHolder.config.s3UploadFilePath;
var s3 = new AWS.S3();
var params = {
Bucket: 'bucket_name'
Key: 's3UploadFilePath',
Body: bufferObject,
CacheControl:'public, max-age=86400'
}
s3.upload(params, function(err, data) {
if (err) {
console.log("Error at uploadCSVFileOnS3Bucket function",err);
next(err);
} else {
console.log("File uploaded Successfully");
next(null, filePath);
}
});
};
Run Code Online (Sandbox Code Playgroud)
另外,我正在使用“json2csv”npm 模块从 JSON 生成 csv 文件内容。
下面是代码:
var generateCSVFile = function(next,callback,csvFileContent) {
console.log("generateCSVFile function started",csvFileContent);
if(csvFileContent && csvFileContent.length>0) {
var fields = ['field1','field2','field3',........];
var csv = json2csv({ data: csvFileContent, fields: fields });
console.log('created',csv);
next(null,csv);
}
else {
next(null,[]);
}
}
Run Code Online (Sandbox Code Playgroud)
请让我们知道上面的代码哪里出错了。
嗨,我再次尝试使用以下标题值,它对我有用。下面是代码:
var s3 = new AWS.S3();
var params = {
Bucket: bucketName,
Key: filePath,
Body: csvFileContent,
ContentType: 'application/octet-stream',
ContentDisposition: contentDisposition(filePath, {
type: 'inline'
}),
CacheControl: 'public, max-age=86400'
}
s3.putObject(params, function(err, data) {
if (err) {
console.log("Error at uploadCSVFileOnS3Bucket function", err);
next(err);
} else {
console.log("File uploaded Successfully");
next(null, filePath);
}
});
Run Code Online (Sandbox Code Playgroud)
在您的参数中添加 ContentDisposition: 'attachment' 。
否则你也可以读取文件并上传到 s3
fs.readFile(FILEPATH, function(err, file_buffer) {
var params = {
Bucket: //bucketname,
Key:key,
ContentDisposition: 'attachment',
Body: file_buffer
};
s3.upload(params, function(err, data) {
if (err) {
console.log("Error in upload");
callback(err, null)
}
if (data) {
console.log("Upload Success", data);
callback(null, data)
}
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9283 次 |
| 最近记录: |