Skip to main content

阿里云 OSS Python SDK(支持异步)

English README

概述

基于 httpx 的阿里云对象存储 Python SDK,支持异步操作并提供类型提示。

[!Note]

  • 此版本不包含 osscmd 命令行工具。
  • 此版本仅支持 Python 3.10 及以上版本。
  • 此版本仅支持 V4 签名。

运行环境

Python 3.10 及以上版本。

安装

通过 PIP 安装官方发布版本:

pip install aliyun-oss-x

如果需要使用阿里云 KMS 加密,请安装 aliyun-kms 扩展包:

pip install aliyun-oss-x[aliyun-kms]

快速入门

同步用法

import aliyun_oss_x

endpoint = 'http://oss-cn-hangzhou.aliyuncs.com' # 假设你的存储桶位于杭州地区

auth = aliyun_oss_x.Auth('<你的 AccessKeyID>', '<你的 AccessKeySecret>')
bucket = aliyun_oss_x.Bucket(auth, endpoint, '<你的存储桶名称>')

# 存储桶中的对象键为 story.txt
key = 'story.txt'

# 上传
bucket.put_object(key, 'Ali Baba 是一个快乐的青年。')

# 下载
bucket.get_object(key).read()

# 删除
bucket.delete_object(key)

# 遍历存储桶中的所有对象
for object_info in aliyun_oss_x.ObjectIterator(bucket):
    print(object_info.key)

异步用法

import asyncio

import aliyun_oss_x

endpoint = 'http://oss-cn-hangzhou.aliyuncs.com' # 假设你的存储桶位于杭州地区

auth = aliyun_oss_x.Auth('<你的 AccessKeyID>', '<你的 AccessKeySecret>')
bucket = aliyun_oss_x.AsyncBucket(auth, endpoint, '<你的存储桶名称>', region="cn-hangzhou")

async def main():
    # 存储桶中的对象键为 story.txt
    key = 'story.txt'

    # 上传
    await bucket.put_object(key, 'Ali Baba 是一个快乐的青年。')

    # 下载
    await bucket.get_object(key).read()

    # 删除
    await bucket.delete_object(key)

    # 遍历存储桶中的所有对象
    async for object_info in aliyun_oss_x.AsyncObjectIterator(bucket):
        print(object_info.key)

asyncio.run(main())

更多示例请参考 "examples" 目录下的代码。

错误处理

除非另有说明,Python SDK 接口在出错时会抛出异常(参见 aliyun_oss_x.exceptions 子模块)。以下是一个示例:

try:
    result = bucket.get_object(key)
    print(result.read())
except aliyun_oss_x.exceptions.NoSuchKey as e:
    print('{0} 未找到:http_status={1}, request_id={2}'.format(key, e.status, e.request_id))

设置日志

以下代码可以设置 'aliyun_oss_x' 的日志级别:

import logging
logging.getLogger('aliyun_oss_x').setLevel(logging.WARNING)

测试

首先通过环境变量设置测试所需的 AccessKeyId、AccessKeySecret、endpoint 和 bucket 信息(不要使用生产环境的存储桶)。 以 Linux 系统为例:

export OSS_TEST_ACCESS_KEY_ID=<AccessKeyId>
export OSS_TEST_ACCESS_KEY_SECRET=<AccessKeySecret>
export OSS_TEST_ENDPOINT=<endpoint>
export OSS_TEST_BUCKET=<bucket>

export OSS_TEST_STS_ID=<用于测试 STS 的 AccessKeyId>
export OSS_TEST_STS_KEY=<用于测试 STS 的 AccessKeySecret>
export OSS_TEST_STS_ARN=<用于测试 STS 的角色 ARN>

按以下方式运行测试:

nosetests                          # 首先安装 nose

更多资源

联系我们

许可证

Metadata

Release files for aliyun-oss-x 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aliyun-oss-x 0.1.8
File Size Uploaded
aliyun_oss_x-0.1.8.tar.gz 148.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aliyun-oss-x 0.1.8
File Interpreter ABI Platform
aliyun_oss_x-0.1.8-py3-none-any.whl Python 3 none any Details

Total release size: 317.1 kB

Release files / aliyun_oss_x-0.1.8.tar.gz

Download URL aliyun_oss_x-0.1.8.tar.gz
Size 148.8 kB
Tags Source
SHA-256 checksum
How to use checksums
da2c39a9aa1d9c1ea99e450a3aadc1c34f8dca0c35dd47aecffe977188618369
BLAKE2b-256 checksum
How to use checksums
bf6b649323c7c98ae420a02af5506405cc2bb11b3f1680d932907944be640c7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.21.0 CPython/3.11.6 Windows/10

Release files / aliyun_oss_x-0.1.8-py3-none-any.whl

Download URL aliyun_oss_x-0.1.8-py3-none-any.whl
Size 168.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a45793c8d505e093285dc0215c84e44f4c602c34ab9fec3cc01e996ae0514a71
BLAKE2b-256 checksum
How to use checksums
41fb9d18f220332bf7a3d5b5909a62230570a50bf2f0716521b04127f48e6165
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.21.0 CPython/3.11.6 Windows/10

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page