Skip to main content

一个支持阿里云OSS和MinIO的对象存储抽象包

Project description

FiuAI S3

一个支持阿里云OSS和MinIO的对象存储抽象包,提供了统一的接口来操作不同的对象存储服务。

特性

  • 支持阿里云OSS和MinIO存储服务
  • 统一的接口设计
  • 工厂模式实现,易于扩展
  • 完整的类型提示
  • 详细的日志记录
  • 异常处理机制

安装

pip install fiuai-s3

快速开始

初始化存储

# file: utils/s3.py
from fiuai_s3 import ObjectStorageFactory
from config.app_config import get_settings

ObjectStorageFactory.initialize(
        # 初始化对象存储
        provider=get_settings().object_storage_config.provider,
        bucket_name=get_settings().object_storage_config.bucket_name,
        endpoint=get_settings().object_storage_config.endpoint,
        access_key=get_settings().object_storage_config.access_key,
        secret_key=get_settings().object_storage_config.secret_key,
        temp_dir=get_settings().object_storage_config.s3_temp_dir,
        use_https=get_settings().object_storage_config.s3_use_https
    
)
S3_Client = ObjectStorageFactory.get_instance()

使用存储实例

# file: app.py
from utils.s3 import S3_Client

# 上传文件
S3_Client.upload_file("test.txt", b"Hello World")

# 下载文件
data = S3_Client.download_file("test.txt")

# 删除文件
S3_Client.delete_file("test.txt")

# 列出文件
files = S3_Client.list_files(prefix="test/")

单据文件管理(推荐业务用法)

支持业务身份(auth_tenant_id, auth_company_id, doc_id)在实例初始化时注入(可为空),各操作方法参数可选,若不传则使用实例属性,若传则覆盖。

初始化带业务身份

from fiuai_s3 import ObjectStorageFactory
from fiuai_s3.object_storage import StorageConfig

config = StorageConfig(
    provider="minio",
    bucket_name="dev",
    endpoint="http://127.0.0.1:19000",
    access_key="devdevdev",
    secret_key="devdevdev"
)
# 业务身份可选
S3_Client = ObjectStorageFactory.create_storage(
    config,
    auth_tenant_id="t1",
    auth_company_id="c1",
    doc_id="d1"
)

上传单据文件

# 方法参数可选,优先级高于实例属性
S3_Client.upload_doc_file(
    filename="发票.pdf",
    data=b"...文件内容...",
    tags={"业务类型": "发票", "年份": "2024"}
    # auth_tenant_id、auth_company_id、doc_id 可不传,使用实例属性
)

下载单据文件

data = S3_Client.download_doc_file(
    filename="发票.pdf"
    # auth_tenant_id、auth_company_id、doc_id 可不传,使用实例属性
)

列出单据下所有文件

files = S3_Client.list_doc_files()
  • 存储路径自动为:bucketname/auth_tenant_id/auth_company_id/doc_id/filename
  • tags会自动打到对象存储(Alicloud用OSS Tagging,Minio用metadata)
  • 支持实例初始化时设置默认auth_tenant_id、auth_company_id、doc_id,调用时可覆盖
  • 缺失必要参数会抛出异常

配置参数

参数 类型 必填 默认值 说明
provider str - 存储提供商,支持 "alicloud" 或 "minio"
bucket_name str - 存储桶名称
endpoint str - 存储服务端点
access_key str - 访问密钥
secret_key str - 密钥
temp_dir str "temp/" 临时目录
use_https bool False 是否使用HTTPS

开发

安装开发依赖

uv pip install .

运行测试

python -m pytest tests/

许可证

MIT License

作者

贡献

欢迎提交 Issue 和 Pull Request!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fiuai_s3-0.3.1.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fiuai_s3-0.3.1-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file fiuai_s3-0.3.1.tar.gz.

File metadata

  • Download URL: fiuai_s3-0.3.1.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.8

File hashes

Hashes for fiuai_s3-0.3.1.tar.gz
Algorithm Hash digest
SHA256 7aea5865d228ad41909fa24f71c2f8e9bd31edc89cf6396c39ce4266fc5ba359
MD5 d7c7a86d8880d5fcc15c69b7f1a3677a
BLAKE2b-256 f04a0a5bfa919201fe4d5617dda54328cec3bd3ba321072ea4a3dd16fd78b990

See more details on using hashes here.

File details

Details for the file fiuai_s3-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: fiuai_s3-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.8

File hashes

Hashes for fiuai_s3-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 75593d3be60b468ed3a68ee41aa365ff69c968a7c3c2ff00a80b5e1fc77f8673
MD5 bc856976d00d963c466d92a3da281ea3
BLAKE2b-256 61a7862aca7ce331c0856f55e95166ba8842c25aa18df946783987a0c94a84ac

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page