• python调用seafile接口上传文件到seafile


    我的 环境:seafile 8.0.3

    其他版本的接口调用还是有区别的!特别是7之前的接口和新版本接口有些参数不一样了。

    1. #!/usr/bin/env python
    2. # -*- coding: utf-8 -*-
    3. # @Time : 2023/10/26 17:39
    4. # @Author : chentufeng
    5. # @File : upload_seafile.py
    6. # @desc : 上传文件到云盘
    7. # !/usr/bin/env python3
    8. # -*- coding: utf-8 -*-
    9. # 这是python2的。python3应该也通用,可能要小改一下,但接口调用一样
    10. import os
    11. import json
    12. import requests
    13. import sys
    14. import commands
    15. reload(sys)
    16. sys.setdefaultencoding('utf-8')
    17. class UploadFile:
    18. def __init__(self, url, username, password, repo_id):
    19. self.url = url
    20. self.data = {'username': username, 'password': password}
    21. self.token_url = '%s/api2/auth-token/'%url
    22. self.token = self.get_token()
    23. self.upload_link = '%s/api2/repos/%s/upload-link/'%(url, repo_id)
    24. # 获取上传链接
    25. @staticmethod
    26. def _get_upload_link(url, token):
    27. resp = requests.get(url, headers={'Authorization': 'Token {token}'.format(token=token)})
    28. print(resp)
    29. return resp.json()
    30. # 获取token
    31. def get_token(self):
    32. try:
    33. print('>>>>>getting token...')
    34. response = requests.post(self.token_url, data=self.data)
    35. token = json.loads(response.content.decode())
    36. print(token)
    37. return token['token']
    38. except KeyError as e:
    39. print('user login failed!')
    40. print(e)
    41. # 上传文件
    42. def upload_file(self, filepath, relative_path):
    43. # parent_dir 参数默认为空,应该是 seafile 8.0.3 开始,7之前是没有relative_path 参数的。
    44. print('>>>>>uploading %s...' % filepath)
    45. self.upload_link = self._get_upload_link(self.upload_link, self.token)
    46. print("上传链接是: %s"%self.upload_link)
    47. # print(folderpath)
    48. response = requests.post(
    49. self.upload_link, data={'filename': os.path.basename(filepath),
    50. 'parent_dir': '', 'relative_path': relative_path, 'replace': 1},
    51. files={'file': open(filepath, 'rb')},
    52. headers={'Authorization': 'Token {token}'.format(token=self.token)}
    53. )
    54. print(str(response))
    55. if str(response) == '':
    56. print('>>>>>文件[%s]上传 success!'%filepath)
    57. else:
    58. print('>>>>>文件[%s]上传 failed!'%filepath)
    59. if __name__ == '__main__':
    60. upload_file_path = "/data/aa.txt" # 需要上传的文件本地路径
    61. relative_path = "test" # 指定要上传到云盘哪个路径
    62. base_url = 'https://yunpan.com'
    63. repo_id = 'xxxx' # 资料库ID
    64. username = 'xxxx' # 用户名
    65. password = 'xxxx' # 密码
    66. f = UploadFile(base_url, username, password, repo_id)
    67. f.upload_file(upload_file_path, relative_path)

  • 相关阅读:
    后台管理系统的权限以及vue处理权限的思路
    AI硬件:显卡 vs. 处理器 vs. 量子计算机
    vue2 维护状态key的作⽤和原理
    <C++> 智能指针
    Kubernetes 与 Calico 版本对比
    GitHub 上有什么好玩又有挑战的前端项目?
    VisualDL的认识
    代码随想录训练营day56, 两个字符串的删除操作, 编辑字符
    简单运用多态性实现动态联编
    第十章:字典树(trie)与并查集
  • 原文地址:https://blog.csdn.net/MYF_12/article/details/134077641