Skip to main content

小阳工具库Oss模块

Project description

阿里云OSS工具库

一个简洁易用的阿里云对象存储服务(OSS) Python SDK封装库,提供常用OSS操作的便捷接口。

功能特性

  • ✅ 文件上传/下载
  • ✅ 字符串内容上传
  • ✅ 范围下载(Range GET)- 支持增量下载
  • ✅ 分页列举对象
  • ✅ 批量删除对象
  • ✅ 完整的类型提示支持
  • ✅ 详细的中文文档

安装

pip install xy_utilities_oss

快速开始

初始化客户端

from xy_utilities_oss import OssHelper
from pathlib import Path

# 配置OSS连接信息
oss_config = {
    '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客户端实例
oss = OssHelper(**oss_config)

上传文件

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

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

下载文件

# 下载到内存
content = 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)

# 范围下载(从指定字节位置开始)
data = oss.download_range('remote/path/large_file.bin', start_byte=1024)

对象管理

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

# 获取对象大小
size = oss.get_object_size('remote/path/file.txt')

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

# 批量删除对象
oss.delete('remote/path/file1.txt', 'remote/path/file2.txt', 'remote/path/file3.txt')

列举对象

# 分页列举对象
pages = oss.list_objects_paginator(prefix='remote/path/', max_keys=100)

for page in pages:
    for obj in page.contents:
        print(f'Object: {obj.key}, Size: {obj.size}, Last Modified: {obj.last_modified}')

API文档

OssHelper

主要操作类,提供所有OSS操作接口。

方法列表

  • upload_file(key, filename, **kwargs) - 上传本地文件
  • put_object(key, string, **kwargs) - 上传字符串内容
  • download(key, **kwargs) - 下载对象到内存
  • download_file(key, filename, overwrite=False, **kwargs) - 下载对象到文件
  • download_range(key, start_byte, **kwargs) - 范围下载
  • is_object_exist(key, **kwargs) - 判断对象是否存在
  • get_object_size(key, **kwargs) - 获取对象大小
  • list_objects_paginator(**kwargs) - 分页列举对象
  • delete(*keys, **kwargs) - 删除对象

更多详细说明请查看代码中的文档字符串。

开发环境设置

Conda

conda create --name xy_utilities_oss -y python=3.10 && \
conda activate xy_utilities_oss && \
pip install build toml twine && \
pip install -r <(python -c "import toml; print('\n'.join(toml.load('pyproject.toml')['project']['dependencies']))")

发布

# 清理旧构建
sudo rm -rf dist *.egg-info && \
python -m build

# 发布到阿里云PyPI
twine upload -r packages-pypi dist/*

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

作者

Colin Chang - zhangcheng@xymind.cn

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.1.tar.gz (5.5 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.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xy_utilities_oss-1.0.1.tar.gz
  • Upload date:
  • Size: 5.5 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.1.tar.gz
Algorithm Hash digest
SHA256 5253770f6db75ebb0dec336a9e03ef706ecb4e3c4c4796634fe5a03438ddb8bd
MD5 20fb0192c1bf961e5509c707c83f732a
BLAKE2b-256 5f791d409ae01c47425c524b1cb1a32e8bdb629269d2a4c4c2af20db3af50c71

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for xy_utilities_oss-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7376f440751dcd7b04e84738c08f97abee90b9da85ecdbc0de27e01ef09406c9
MD5 7456a99056b7f9a2b453ff0e41379ff1
BLAKE2b-256 155863c4d43ea260cd4937a9101ac5b0aff2e8e8285f18250857a92047af8261

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