• 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)

  • 相关阅读:
    请大家一定不要像我们公司这样打印log日志
    Kubernetes(k8s)的Namespace和Pod实战入门
    6-2 分治法求解金块问题
    【NLP入门教程】十八、支持向量机(Support Vector Machines)
    17.12 事务处理(血干JAVA系类)
    P1394 山上的国度 题解
    Spring MVC 中 HttpMessageConverter 转换器
    深度剖析React懒加载原理
    uniApp跨页面通讯$on、$emit、$once、$off使用场景及技巧
    重新实现hashCode()方法
  • 原文地址:https://blog.csdn.net/MYF_12/article/details/134077641