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
with BaiduPanClient(
client_id="your_client_id",
client_secret="your_client_secret",
app_name="your_app_name",
) as client:
if not client.is_authenticated:
print(f"请在浏览器打开: {client.auth_url}")
code = input("请输入授权码: ")
client.fetch_token(code)
Token 会自动保存到 ~/.baidupan/ 目录,下次启动无需重新授权。
3. 使用 API
# 查询空间配额
q = client.quota()
print(f"已用: {q.used / 1024**3:.1f} GB / 总计: {q.total / 1024**3:.1f} GB")
# 上传文件 - 支持 Path / bytes / Generator
result = client.upload(Path("local_file.txt"))
result = client.upload(Path("data.bin"), remote_path="/backup/data.bin")
result = client.upload(b"hello world", remote_path="/hello.txt")
# 获取文件元信息
metas = client.file_metas([result.fs_id])
dlink = metas[0].dlink
# 下载文件到本地路径
client.download_to_file(dlink, Path("downloaded.txt"))
# 流式下载(适合大文件或自定义处理)
for chunk in client.download_stream(dlink):
process(chunk)
# 带断点续传的流式下载
for chunk in client.download_stream_with_range(dlink, range=(1024, None)):
process(chunk)
# 列出文件
items = client.list_files("/apps/your_app_name/")
for item in items:
print(f"{'[DIR]' if item.is_dir else ' '} {item.filename} ({item.size} bytes)")
# 删除文件
client.delete(["/apps/your_app_name/old_file.txt"])
API 参考
BaiduPanClient
| 方法 | 说明 |
|---|---|
upload(source, remote_path="") |
上传数据。source 支持 Path / bytes / Generator[bytes];remote_path 为远程路径字符串 |
download_to_file(dlink, local_path, *, chunk_size=1048576) |
下载文件到本地路径,返回 Path |
download_stream(dlink, *, chunk_size=1048576) |
流式下载,返回 Generator[bytes] |
download_stream_with_range(dlink, range, *, chunk_size=1048576, max_retries=5) |
带断点续传的流式下载,range 为 (start, end) 元组 |
quota() |
获取空间配额,返回 ApiQuotaInfo |
file_metas(fsids) |
获取文件元信息(含 dlink),返回 list[ApiFileMeta] |
list_files(path="/") |
列出目录文件,返回 list[ApiFileListItem] |
delete(paths) |
批量删除文件 |
fetch_token(code) |
通过授权码获取 Token |
refresh_token() |
刷新 Token |
is_authenticated |
是否已认证(属性) |
close() |
关闭客户端 |
异常体系
BaiduPanError
├── BaiduPanNetworkError # 网络通信异常
├── BaiduPanAPIError # API 业务异常 (errno, errmsg, request_id)
└── TokenExpiredError # Token 过期或无效
所有异常均继承自 BaiduPanError,方便统一捕获。
特性
- 自动重试 — 网络请求自动重试(可配置次数),支持指数退避
- Token 管理 — 自动持久化、自动刷新、文件权限限制
- 分片上传 — 大文件自动分片上传(4MB 分片)
- 流式下载 — 支持断点续传、Range 分片、指数重试
- 类型安全 — 完整类型标注,支持 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.2.0.tar.gz
(24.8 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.2.0-py3-none-any.whl
(18.5 kB
view details)
File details
Details for the file bdds-0.2.0.tar.gz.
File metadata
- Download URL: bdds-0.2.0.tar.gz
- Upload date:
- Size: 24.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a4d09d3b907c12ad88ba18fab28e96cc567a2df433b17979e0239d5aeb2e42b
|
|
| MD5 |
c0d21678309aa35d3651c32d5439dc75
|
|
| BLAKE2b-256 |
4fc6e03f94ba03e599a50e9459324b8b055c7c4e1794780365c86f995188b475
|
File details
Details for the file bdds-0.2.0-py3-none-any.whl.
File metadata
- Download URL: bdds-0.2.0-py3-none-any.whl
- Upload date:
- Size: 18.5 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 |
779579ffd150b59800eae43d328714ee039c8be405276df3f8ace2e843841ff1
|
|
| MD5 |
b4d13c91af52862f514c7f43c4c540d5
|
|
| BLAKE2b-256 |
f0dbfe47f2e72bbaa32abcd20ca31dd238c9d69b6eae2acb44fc8702d7a1b609
|