使用节点 fs 从 aws s3 存储桶读取文件 [英] Read file from aws s3 bucket using node fs
本文介绍了使用节点 fs 从 aws s3 存储桶读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用
fs.readFile(file, function (err, contents) {
var myLines = contents.Body.toString().split('
')
})
我已经能够使用节点 aws-sdk 下载和上传文件,但我不知道如何简单地读取它并解析内容.
I've been able to download and upload a file using the node aws-sdk, but I am at a loss as to how to simply read it and parse the contents.
以下是我如何从 s3 读取文件的示例:
Here is an example of how I am reading the file from s3:
var s3 = new AWS.S3();
var params = {Bucket: 'myBucket', Key: 'myKey.csv'}
var s3file = s3.getObject(params)
推荐答案
您有几个选择.您可以包含一个回调作为第二个参数,它将使用任何错误消息和对象进行调用.这个 示例 直接来自 AWS 文档:
You have a couple options. You can include a callback as a second argument, which will be invoked with any error message and the object. This example is straight from the AWS documentation:
s3.getObject(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
或者,您可以将输出转换为流.AWS 文档中还有一个 example:
Alternatively, you can convert the output to a stream. There's also an example in the AWS documentation:
var s3 = new AWS.S3({apiVersion: '2006-03-01'});
var params = {Bucket: 'myBucket', Key: 'myImageFile.jpg'};
var file = require('fs').createWriteStream('/path/to/file.jpg');
s3.getObject(params).createReadStream().pipe(file);
这篇关于使用节点 fs 从 aws s3 存储桶读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文