文件(夹)上传
上传文件(夹)¶
Upload
¶
Source code in src/cpanbaidu/Upload.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
__init__
¶
precreate
¶
precreate(
path: str,
size: int,
isdir: Literal[0, 1],
block_list: list[str] | str,
rtype: Literal[1, 2, 3] = 1,
uploadid: Optional[str] = None,
content_md5: Optional[str] = None,
slice_md5: Optional[str] = None,
local_ctime: Optional[int] = None,
local_mtime: Optional[int] = None,
) -> dict[str, Any] | None
预上传
预上传是通知网盘云端新建一个上传任务, 网盘云端返回唯一ID uploadid 来标识此上传任务.
对应百度的API接口: https://pan.baidu.com/union/doc/3ksg0s9r7
注意事项
执行示例代码时,请将示例代码中的access_token参数值替换为自行获取的access_token 云端文件重命名策略说明:当云端已有文件test.txt时,新的文件名字为test(1).txt;当云端已有目录 /dir时,新的目录名字为/dir(1)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
上传的文件或目录的路径 |
required |
size
|
int
|
文件和目录两种情况:上传文件时, 表示文件的大小, 单位B;上传目录时, 表示目录的大小, 目录的话大小默认为0 |
required |
isdir
|
Literal[0, 1]
|
是否为目录, 0 文件, 1 目录 |
required |
block_list
|
list[str] | str
|
分片上传时, 分片列表, 分片大小为4MB, 最大支持10000个分片 |
required |
rtype
|
Literal[1, 2, 3]
|
文件命名策略. 1 表示当path冲突时, 进行重命名 //2 表示当path冲突且block_list不同时, 进行重命名 |
1
|
uploadid
|
Optional[str]
|
上传ID |
None
|
content_md5
|
Optional[str]
|
文件MD5, 32位小写 |
None
|
slice_md5
|
Optional[str]
|
文件校验段的MD5, 32位小写, 校验段对应文件前256KB |
None
|
local_ctime
|
Optional[int]
|
客户端创建时间, 默认为当前时间戳 |
None
|
local_mtime
|
Optional[int]
|
客户端修改时间, 默认为当前时间戳 |
None
|
Source code in src/cpanbaidu/Upload.py
upload_part
¶
upload_part(
url: str,
path: str,
uploadid: str,
partseq: int,
files: Any,
) -> dict[str, Any] | None
分片上传 本接口用于将本地文件上传到网盘云端服务器.
文件分两种类型:小文件, 是指文件大小小于等于4MB的文件, 成功调用一次本接口后, 表示分片上传阶段完成;大文件, 是指文件大小大于4MB的文件, 需要先将文件按照4MB大小进行切分, 然后针对切分后的分片列表, 逐个分片进行上传, 分片列表的分片全部成功上传后, 表示分片上传阶段完成.
根据不同的用户等级有不同的限制
对应百度的API接口: https://pan.baidu.com/union/doc/nksg0s9vi
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
上传域名(从 locateupload 接口获取) |
required |
path
|
str
|
上传的文件的路径 |
required |
uploadid
|
str
|
上传ID |
required |
partseq
|
int
|
分片序号, 从0开始 |
required |
files
|
Any
|
上传的文件内容 |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
分片上传结果 |
Source code in src/cpanbaidu/Upload.py
create
¶
create(
path: str,
size: str,
isdir: Literal["0", "1"],
block_list: list[str] | str,
uploadid: str,
rtype: Literal[1, 2, 3] = 1,
local_ctime: Optional[int] = None,
local_mtime: Optional[int] = None,
zip_quality: Optional[Literal[50, 70, 100]] = None,
zip_sign: Optional[int] = None,
is_revision: Optional[int] = 0,
mode: Optional[Literal[0, 1, 2, 3, 4, 5]] = None,
exif_info: Optional[str] = None,
) -> dict[str, Any] | None
创建文件
本接口用于将多个文件分片合并成一个文件, 生成文件基本信息, 完成文件的上传最后一步.
对应百度的API接口: https://pan.baidu.com/union/doc/rksg0sa17
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
上传的文件或目录的路径 |
required |
size
|
str
|
文件和目录两种情况:上传文件时, 表示文件的大小, 单位B;上传目录时, 表示目录的大小, 目录的话大小默认为0 |
required |
isdir
|
Literal[0, 1]
|
是否为目录, 0 文件, 1 目录 |
required |
block_list
|
list[str] | str
|
文件各分片md5数组的json串 |
required |
uploadid
|
str
|
预上传precreate接口下发的uploadid |
required |
rtype
|
Literal[1, 2, 3]
|
文件命名策略.
|
1
|
local_ctime
|
Optional[int]
|
客户端创建时间, 默认为当前时间戳 |
None
|
local_mtime
|
Optional[int]
|
客户端修改时间, 默认为当前时间戳 |
None
|
zip_quality
|
Optional[Literal[50, 70, 100]]
|
图片压缩程度, 有效值50、70、100, (与zip_sign一起使用) |
None
|
zip_sign
|
Optional[int]
|
未压缩原始图片文件真实md5(与zip_quality一起使用) |
None
|
is_revision
|
Optional[int]
|
是否需要多版本支持, 1为支持, 0为不支持, 默认为0 (带此参数会忽略重命名策略) |
0
|
mode
|
Optional[Literal[0, 1, 2, 3, 4, 5]]
|
上传方式
|
None
|
exif_info
|
Optional[str]
|
json字符串, orientation、width、height、recovery为必传字段, 其他字段如果没有可以不传 |
None
|
Source code in src/cpanbaidu/Upload.py
upload_simple
¶
upload_simple(
url: str,
path: str,
file: Any,
ondup: Literal[
"newcopy", "overwrite", "fail"
] = "newcopy",
) -> dict[str, Any] | None
简单上传文件
本接口用于将本地小文件上传到网盘云端服务器.
对应百度的API接口: https://pan.baidu.com/union/doc/Llvw5hfnm
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
上传域名(从 locateupload 接口获取) |
required |
path
|
str
|
上传的文件绝对路径 |
required |
ondup
|
Literal['newcopy', 'overwrite', 'fail']
|
文件冲突处理策略, 上传的文件绝对路径冲突时的策略。
|
'newcopy'
|
file
|
Any
|
上传的文件内容 |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
简单上传结果 |
Source code in src/cpanbaidu/Upload.py
locateupload
¶
获取上传域名
本接口用于获取上传域名.
上传文件数据时, 需要先通过此接口获取上传域名. 可使用返回结果servers字段中的 https 协议的任意一个域名.
对应百度的API接口: https://pan.baidu.com/union/doc/Mlvw5hfnr
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
上传后使用的文件绝对路径 |
required |
uploadid
|
str
|
上传ID |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
包含上传域名的字典 |
Source code in src/cpanbaidu/Upload.py
UploadFile
¶
上传文件类, 负责将本地文件分片上传到百度网盘. (使用多线程上传)
Attributes:
| Name | Type | Description |
|---|---|---|
up |
Upload
|
上传对象的实例. |
Example:
Source code in src/cpanbaidu/Upload.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 | |
__init__
¶
上传文件类
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth
|
Auth
|
Auth 类实例 |
required |
userinfo
|
Optional[UserInfoModel]
|
用户信息模型实例 |
None
|
upload_part
¶
upload_part(
server_url: str,
upload_path: str,
uploadid: str,
idx: int,
chunk: bytes,
expected_md5: str,
progress: dict,
show_progress: bool = True,
) -> int
上传单个文件分片并更新上传进度.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_url
|
str
|
上传服务器的 URL. |
required |
upload_path
|
str
|
文件在网盘中的目标路径. |
required |
uploadid
|
str
|
上传会话的 ID. |
required |
idx
|
int
|
当前分片的索引. |
required |
chunk
|
bytes
|
当前分片的二进制数据. |
required |
expected_md5
|
str
|
当前分片的预期 MD5 值. |
required |
progress
|
Dict[str, object]
|
包含上传进度信息的字典. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
成功上传的分片索引. |
Raises:
| Type | Description |
|---|---|
Exception
|
如果上传失败或 MD5 校验不一致. |
Source code in src/cpanbaidu/Upload.py
upload_folder
¶
upload_folder(
local_folder: str,
upload_path: str,
rtype: Literal[1, 2, 3] = 1,
file_max_workers: Optional[int] = None,
chunk_max_workers: Optional[int] = None,
bs: Literal[4, 16, 32] = 4,
show_progress: bool = False,
exclude_patterns: Optional[list[str]] = None,
) -> dict[str, Any]
递归上传本地文件夹到百度网盘(使用多线程并发上传多个文件).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_folder
|
str
|
本地文件夹的路径. |
required |
upload_path
|
str
|
文件夹在网盘中的目标路径. |
required |
rtype
|
Literal[1, 2, 3]
|
文件命名策略, 默认为 1. 1: 当path冲突时, 进行重命名 2: 当path冲突且block_list不同时, 进行重命名 3: 当云端存在同名文件时, 对该文件进行覆盖 |
1
|
file_max_workers
|
int
|
文件级并发线程数, 默认为 CPU核心数-1. |
None
|
chunk_max_workers
|
int
|
单个文件分片上传的最大并发线程数, 默认为 CPU核心数-1. |
None
|
bs
|
Literal[4, 16, 32]
|
分片大小, 单位为 MB, 默认为 4MB. |
4
|
show_progress
|
bool
|
是否显示上传进度, 默认为 False. |
False
|
exclude_patterns
|
list[str]
|
要排除的文件/文件夹模式列表,支持通配符,如 ['*.tmp', 'pycache', '.git'] |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Any]
|
包含上传结果的字典,包括成功和失败的文件列表 |
Note
- 使用两层多线程: 文件级并发 + 每个文件内部的分片并发
- file_max_workers: 控制同时上传多少个文件
- chunk_max_workers: 控制每个文件内部同时上传多少个分片
Example
Source code in src/cpanbaidu/Upload.py
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 | |
upload_file
¶
upload_file(
local_filename: str,
upload_path: str,
isdir: Literal[0, 1] = 0,
rtype: Literal[1, 2, 3] = 1,
max_workers: Optional[int] = None,
bs: Literal[4, 16, 32] = 4,
show_progress: bool = True,
) -> None | dict
上传单个文件到百度网盘,采用多线程分片上传方式.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_filename
|
str
|
本地文件的路径. |
required |
upload_path
|
str
|
文件在网盘中的目标路径. |
required |
isdir
|
Literal[0, 1]
|
是否为目录,默认为 0(文件). |
0
|
rtype
|
Literal[1, 2, 3]
|
文件命名策略,默认为 1. 1: 当path冲突时, 进行重命名 2: 当path冲突且block_list不同时, 进行重命名 3: 当云端存在同名文件时, 对该文件进行覆盖 |
1
|
max_workers
|
int
|
分片上传的最大并发线程数,默认为 CPU核心数-1. |
None
|
bs
|
Literal[4, 16, 32]
|
分片大小,单位为 MB,默认为 4MB. |
4
|
show_progress
|
bool
|
是否显示上传进度,默认为 True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
None | dict
|
包含上传结果的字典 |