nodejs 通过s3fs 处理文件
s3fs 是一个不错的nodejs fs s3 接口实现,我们基于此可以访问s3 文件,可以方便的简化文件存储
参考代码
const s3Fs = require("@hasnat/s3fs")
const fs = require("fs")
const { pipeline } = require("stream")
const bucketPath = 'cats';
const s3Options = {
accessKeyId: 'minio',
secretAccessKey: 'minio123',
endpoint: 'http://127.0.0.1:9002',
s3ForcePathStyle: true, // needed with minio?
signatureVersion: 'v4'
};
const fsImpl = new s3Fs(bucketPath, s3Options);
const srcStream = fs.createReadStream("file2.txt")
const desStream = fsImpl.createWriteStream("/demoapptest/dalongv2.txt")
pipeline(srcStream, desStream, (err) => {
console.log("pipeline result:", err)
})
fsImpl.readdirp('demoapptest').then(function (files) {
console.log(files)
}, function (reason) {
console.log(reason)
});
说明
如果使用pipeline 进行操作会发现callback 是不能执行的,具体原因需要分析下,不然缺少文件状态的记录,很不方便,但是总的来说s3fs 是一个好的工具包
参考资料
https://docs.min.io/docs/how-to-use-aws-sdk-for-javascript-with-minio-server.html
https://github.com/hasnat/s3fs
https://www.npmjs.com/package/@hasnat/s3fs