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

posted on   荣锋亮  阅读(105)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-06-03 Lightning Web Components 组件样式(四)
2019-06-03 Lightning Web Components html_templates(三)
2019-06-03 Lightning Web Components 开发指南(二)
2019-06-03 Lightning Web Components 安装试用(一)
2019-06-03 Lightning Web Components 来自salesforce 的web 组件化解决方案
2015-06-03 .net 开源 JavaScript 解析引擎
2014-06-03 sql server CLR

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示