Skip to main content

小阳工具库Oss模块 — 阿里云OSS便捷操作封装

Project description

xy-utilities-oss

PyPI version Python

阿里云 OSS Python SDK 封装库,提供简洁易用的对象存储操作接口。

功能特性

  • 文件上传 / 字符串内容上传
  • 下载到内存、本地文件,或范围下载(Range GET)
  • 检查对象是否存在,获取对象大小
  • 分页列举对象
  • 单个 / 批量删除对象
  • 完整类型提示

安装

pip install xy_utilities_oss

快速开始

初始化

from xy_utilities_oss import OssHelper
from pathlib import Path

oss = OssHelper(
    auth={
        "access_key_id": "your_access_key_id",
        "access_key_secret": "your_access_key_secret",
    },
    bucket={
        "name": "your-bucket-name",
        "endpoint": "https://oss-cn-shanghai.aliyuncs.com",
        "region": "cn-shanghai",
    },
)

上传

# 上传本地文件
oss.upload_file("remote/path/file.txt", Path("local_file.txt"))

# 上传字符串内容
oss.put_object("remote/path/data.json", '{"key": "value"}')

下载

# 下载到内存(返回 bytes)
content: bytes = oss.download("remote/path/file.txt")

# 下载到本地文件
oss.download_file("remote/path/file.txt", Path("local_file.txt"))

# 覆盖已存在的文件
oss.download_file("remote/path/file.txt", Path("local_file.txt"), overwrite=True)

# 范围下载(从指定字节偏移开始,适用于 AppendObject 增量读取)
data: bytes = oss.download_range("remote/path/large_file.bin", start_byte=1024)

对象管理

# 检查对象是否存在
exists: bool = oss.is_object_exist("remote/path/file.txt")

# 获取对象大小(字节),不存在时返回 None
size: int | None = oss.get_object_size("remote/path/file.txt")

# 删除单个对象
oss.delete("remote/path/file.txt")

# 批量删除
oss.delete("remote/path/file1.txt", "remote/path/file2.txt")

列举对象

pages = oss.list_objects_paginator(prefix="remote/path/", max_keys=100)

for page in pages:
    for obj in page.contents:
        print(f"{obj.key}  size={obj.size}  modified={obj.last_modified}")

API 参考

OssHelper(auth, bucket)

方法 说明
upload_file(key, filename, **kwargs) 上传本地文件
put_object(key, content, **kwargs) 上传字符串内容
download(key, **kwargs) → bytes 下载到内存
download_file(key, filename, overwrite, **kwargs) 下载到本地文件
download_range(key, start_byte, **kwargs) → bytes 范围下载
is_object_exist(key, **kwargs) → bool 判断对象是否存在
get_object_size(key, **kwargs) → int | None 获取对象大小
list_objects_paginator(**kwargs) → Iterator 分页列举对象
delete(*keys, **kwargs) 删除一个或多个对象

详细参数说明请参阅各方法的 docstring。

开发环境

conda create -n xy_utilities_oss python=3.11 -y
conda activate xy_utilities_oss
pip install build twine
pip install "alibabacloud-oss-v2>=1.1.3"

发布

# 构建
rm -rf dist *.egg-info
python -m build

# 发布到私有 PyPI
twine upload -r packages-pypi dist/*

# 发布到 PyPI
python -m twine upload dist/*

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

xy_utilities_oss-1.0.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

xy_utilities_oss-1.0.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file xy_utilities_oss-1.0.2.tar.gz.

File metadata

  • Download URL: xy_utilities_oss-1.0.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for xy_utilities_oss-1.0.2.tar.gz
Algorithm Hash digest
SHA256 e9821a5dad6a46acad64d292a6cb4fc6caaeb870f7a3e10c19e9ffc4d89e1377
MD5 21697936a93ae6c4d20a58bbcebb4cc1
BLAKE2b-256 0586378d9999486410c7463d9c30c5568cca23a26e8526a26b073d08b81a41a4

See more details on using hashes here.

File details

Details for the file xy_utilities_oss-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for xy_utilities_oss-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 99079f128f029f2babb77c961d7334ed7953f82be9a78066765614c47f824234
MD5 e1f848c78a595a9d4e267d8e828dd42b
BLAKE2b-256 8d0c986c6d7e4a635e52b32ae7a4099c2629a244df4b88a4c0cb071b77c601f2

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