Baidu Disk SDK - 百度网盘 Python SDK
Project description
bdds
Baidu Disk SDK - 简单、有效、报错清晰的百度网盘 Python SDK。
基于 百度网盘开放平台 API 封装,核心依赖仅 requests。
安装
pip install bdds
快速开始
1. 获取应用凭证
在 百度网盘开放平台 创建应用,获取 client_id、client_secret 和 app_name。
2. 初始化客户端
from pathlib import Path
from bdds import BaiduPanClient
client = BaiduPanClient(
client_id="your_client_id",
client_secret="your_client_secret",
app_name="your_app_name",
)
3. 授权登录
# 首次使用需要授权
if not client.access_token:
print(f"请在浏览器打开: {client.auth_url}")
code = input("请输入授权码: ")
client.fetch_token(code)
Token 会自动保存到 ~/.baidupan/ 目录,下次启动无需重新授权。
4. 使用 API
# 查询空间配额
quota = client.get_quota()
print(f"已用: {quota.used / 1024**3:.1f} GB / 总计: {quota.total / 1024**3:.1f} GB")
# 上传文件 - 支持 Path / bytes / Generator
result = client.upload(Path("local_file.txt"))
result = client.upload(Path("data.bin"), file_to=Path("/backup/data.bin"))
result = client.upload(b"hello world", file_to=Path("/hello.txt"))
# 获取文件元信息
metas = client.get_file_metas([result.fs_id])
dlink = metas[0].dlink
# 下载文件 - file_to=None 返回流式迭代器,file_to=Path 写入文件
path = client.download(dlink, file_to=Path("downloaded.txt"))
# 流式下载(适合大文件或自定义处理)
for chunk in client.download(dlink):
process(chunk)
# 列出文件
items = client.get_file_list("/apps/your_app_name/")
for item in items:
print(f"{'[DIR]' if item.is_dir else ' '} {item.filename} ({item.size} bytes)")
# 删除文件
client.delete_files(["/apps/your_app_name/old_file.txt"])
# 使用完毕后关闭
client.close()
推荐使用上下文管理器自动关闭:
with BaiduPanClient(client_id, client_secret, app_name) as client:
quota = client.get_quota()
# ...
API 参考
BaiduPanClient
| 方法 | 说明 |
|---|---|
upload(file_from, file_to=None) |
上传文件。file_from 支持 Path / bytes / Generator[bytes];file_to 为远程路径 Path,默认使用文件名 |
download(dlink, file_to=None, chunk_size=1048576) |
下载文件。file_to=None 返回字节迭代器;file_to=Path 写入本地文件 |
get_quota() |
获取空间配额,返回 ApiQuotaInfo |
get_file_metas(fsids) |
获取文件元信息,返回 list[ApiFileMeta] |
get_file_list(path) |
列出目录文件,返回 list[ApiFileListItem] |
delete_files(file_paths) |
批量删除文件 |
fetch_token(code) |
通过授权码获取 Token |
refresh_token() |
刷新 Token |
close() |
关闭客户端 |
异常体系
BaiduPanError
├── BaiduPanNetworkError # 网络通信异常
├── BaiduPanAPIError # API 业务异常 (errno, errmsg, request_id)
└── TokenExpiredError # Token 过期或无效
所有异常均继承自 BaiduPanError,方便统一捕获。
特性
- 自动重试 — 网络请求自动重试(可配置次数),支持指数退避
- Token 管理 — 自动持久化、自动刷新、文件权限限制
- 分片上传 — 大文件自动分片上传(4MB 分片)
- 流式下载 — 支持断点续传、指数重试
- 类型安全 — 完整类型标注,支持 PEP 561 (
py.typed) - 报错清晰 — 异常包含
errno、errmsg、request_id、url等上下文
开发
# 安装开发依赖
pip install -e ".[dev]"
# 运行测试
python -m pytest -v
# 构建
pip install build twine
python -m build
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
bdds-0.1.0.tar.gz
(22.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
bdds-0.1.0-py3-none-any.whl
(17.0 kB
view details)
File details
Details for the file bdds-0.1.0.tar.gz.
File metadata
- Download URL: bdds-0.1.0.tar.gz
- Upload date:
- Size: 22.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a355032339550ad2d422c363605e235e8e00bf89aedc10cd0b4626eb6e776530
|
|
| MD5 |
3359b4e362016fa40097595e1fd4a5e4
|
|
| BLAKE2b-256 |
2fd6f280e0686191f3ca352e911bd3ba0a2d0c80f4ec5e34e2c491927fc08e18
|
File details
Details for the file bdds-0.1.0-py3-none-any.whl.
File metadata
- Download URL: bdds-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d9a619e264dfc8c41e181b9d46189a7996a26f915bfe3c8fe755dd957fceaed3
|
|
| MD5 |
8bf318101c4386389538da4a66c1f100
|
|
| BLAKE2b-256 |
fbed57c03b618b583a809bf0ef79394357e388c22150c32e55846d81a7434b88
|