Skip to main content

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

Metadata

Release files for bdds 0.2.0

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

Source distribution (sdist)

Source distribution for bdds 0.2.0
File Size Uploaded Provenance
bdds-0.2.0.tar.gz 24.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for bdds 0.2.0
File Interpreter ABI Platform
bdds-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 43.2 kB

Release files / bdds-0.2.0.tar.gz

Download URL bdds-0.2.0.tar.gz
Size 24.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0a4d09d3b907c12ad88ba18fab28e96cc567a2df433b17979e0239d5aeb2e42b
BLAKE2b-256 checksum
How to use checksums
4fc6e03f94ba03e599a50e9459324b8b055c7c4e1794780365c86f995188b475
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.7

Release files / bdds-0.2.0-py3-none-any.whl

Download URL bdds-0.2.0-py3-none-any.whl
Size 18.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
779579ffd150b59800eae43d328714ee039c8be405276df3f8ace2e843841ff1
BLAKE2b-256 checksum
How to use checksums
f0dbfe47f2e72bbaa32abcd20ca31dd238c9d69b6eae2acb44fc8702d7a1b609
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.7

Security for bdds 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

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