通过ossfs fsspec 扩展访问oss 数据

尽管oss 对于s3 客户端具有兼容性,直接通过s3fs 是可以直接访问oss 的,但是有不少缺陷(比如ls 的支持就有问题)ossfs 是基于oss client 实现的标准
fsspec 扩展,使用上很不错,对于文件系统常见操作支持的很好,以下是一个简单使用说明

参考使用

  • 安装
pip install ossfs 
  • 代码使用
    直接基于了标准fsspec,自动进行protocol 发现
    demo.py
import fsspec
fs = fsspec.filesystem('oss',endpoint='https://oss-cn-beijing.aliyuncs.com',key="xxxxx",secret="xxxx")
ls_fs = fs.ls('/ollama-beijing/')
print(ls_fs)

直接使用OSSFileSystem 类的

import ossfs
fs = ossfs.OSSFileSystem(endpoint='https://oss-cn-beijing.aliyuncs.com',key="xxxxx",secret="xxxx")
ls_fs  = fs.ls('/ollama-beijing/')
print(ls_fs)

说明

基于ossfs 进行oss 的访问还是很方便的,对于一些数据分析场景,需要使用oss 的,尤其是基于python 处理框架的很值得尝试下,当然官方基于fuse 的
ossfs 也是一个不错的选择值得使用下 ,尤其是希望直接挂载s3 数据 为文件系统的

参考资料

https://github.com/fsspec/ossfs
https://filesystem-spec.readthedocs.io/en/latest/usage.html
https://github.com/aliyun/ossfs
https://s3fs.readthedocs.io/en/latest/
https://github.com/fsspec/s3fs

posted on 2024-09-26 08:14  荣锋亮  阅读(7)  评论(0编辑  收藏  举报

导航