file.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # -*- coding: UTF-8 -*-
  2. import esigntool
  3. import requests
  4. from esigntool import esign_run_print_outer
  5. from esigntool.esign_file import fileHelp
  6. # 文件类API
  7. config = esigntool.config() # 初始化配置类
  8. @esign_run_print_outer
  9. def getFileUploadUrl(file):
  10. """
  11. 获取文件上传地址
  12. :return:
  13. """
  14. contentType = "application/pdf" # 声明请求变量
  15. body = {
  16. "contentMd5": file.contentMd5,
  17. "contentType": contentType,
  18. "convert2Pdf": False,
  19. "fileName": file.fileName,
  20. "fileSize": file.fileSize
  21. } # 构建请求参数body体
  22. api_path = "/v3/files/file-upload-url"
  23. method = esigntool.httpMethodEnum.POST
  24. json_headers = esigntool.buildSignJsonHeader(config.appId, config.scert,
  25. method, api_path, body) # 签名并构造签名鉴权json请求头
  26. resp = requests.request(method, config.host + api_path, json=body, headers=json_headers) # 发送请求
  27. fileUploadUrl = resp.json()['data']['fileUploadUrl'] # 获取文件上传路径
  28. print(resp.text)
  29. return fileUploadUrl
  30. @esign_run_print_outer
  31. def fileStreamUpload(binfile, fileUploadUrl):
  32. """
  33. 文件流上传服务器
  34. :return:
  35. """
  36. contentMd5 = file.contentMd5 # 声明请求变量
  37. contentType = "application/pdf" # 声明请求变量
  38. method = esigntool.httpMethodEnum.PUT # 声明请求方法
  39. json_headers = esigntool.buildFileUploadHeader(contentType, contentMd5) # 构建请求头
  40. resp = requests.request(method, fileUploadUrl, data=binfile, headers=json_headers) # 发送请求
  41. print(resp.text)
  42. return resp
  43. @esign_run_print_outer
  44. def keywordsPositions():
  45. """
  46. 追检索文件关键字坐标
  47. :return:
  48. """
  49. fileId = "df********grgter" # 声明请求变量
  50. keyword = "关键字1,关键字2" # 声明请求变量
  51. api_path = "/v3/files/{}/keyword-positions?keywords={}".format(fileId, keyword) # 拼接请求路径
  52. method = esigntool.httpMethodEnum.GET # 声明请求方法
  53. json_headers = esigntool.buildSignJsonHeader(config.appId, config.scert,
  54. method, api_path) # 计算签名并构造签名鉴权json请求头
  55. resp = requests.request(method, config.host + api_path, json=None, headers=json_headers) # 发送请求
  56. print(resp.text)
  57. return resp
  58. if __name__ == '__main__':
  59. fileUrl = "D:\\**文件\\**.pdf" # 请输入本地文件路径
  60. file = fileHelp(fileUrl) # 初始化文件辅助类
  61. fileUploadUrl = getFileUploadUrl(file) # 获取文件上传路径
  62. fileStreamUpload(file.getBinFile(), fileUploadUrl) # 文件流上传
  63. keywordsPositions() # 关键字查询