| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- # -*- coding: UTF-8 -*-
- import esigntool
- import requests
- from esigntool import esign_run_print_outer
- from esigntool.esign_file import fileHelp
- # 文件类API
- config = esigntool.config() # 初始化配置类
- @esign_run_print_outer
- def getFileUploadUrl(file):
- """
- 获取文件上传地址
- :return:
- """
- contentType = "application/pdf" # 声明请求变量
- body = {
- "contentMd5": file.contentMd5,
- "contentType": contentType,
- "convert2Pdf": False,
- "fileName": file.fileName,
- "fileSize": file.fileSize
- } # 构建请求参数body体
- api_path = "/v3/files/file-upload-url"
- method = esigntool.httpMethodEnum.POST
- json_headers = esigntool.buildSignJsonHeader(config.appId, config.scert,
- method, api_path, body) # 签名并构造签名鉴权json请求头
- resp = requests.request(method, config.host + api_path, json=body, headers=json_headers) # 发送请求
- fileUploadUrl = resp.json()['data']['fileUploadUrl'] # 获取文件上传路径
- print(resp.text)
- return fileUploadUrl
- @esign_run_print_outer
- def fileStreamUpload(binfile, fileUploadUrl):
- """
- 文件流上传服务器
- :return:
- """
- contentMd5 = file.contentMd5 # 声明请求变量
- contentType = "application/pdf" # 声明请求变量
- method = esigntool.httpMethodEnum.PUT # 声明请求方法
- json_headers = esigntool.buildFileUploadHeader(contentType, contentMd5) # 构建请求头
- resp = requests.request(method, fileUploadUrl, data=binfile, headers=json_headers) # 发送请求
- print(resp.text)
- return resp
- @esign_run_print_outer
- def keywordsPositions():
- """
- 追检索文件关键字坐标
- :return:
- """
- fileId = "df********grgter" # 声明请求变量
- keyword = "关键字1,关键字2" # 声明请求变量
- api_path = "/v3/files/{}/keyword-positions?keywords={}".format(fileId, keyword) # 拼接请求路径
- method = esigntool.httpMethodEnum.GET # 声明请求方法
- json_headers = esigntool.buildSignJsonHeader(config.appId, config.scert,
- method, api_path) # 计算签名并构造签名鉴权json请求头
- resp = requests.request(method, config.host + api_path, json=None, headers=json_headers) # 发送请求
- print(resp.text)
- return resp
- if __name__ == '__main__':
- fileUrl = "D:\\**文件\\**.pdf" # 请输入本地文件路径
- file = fileHelp(fileUrl) # 初始化文件辅助类
- fileUploadUrl = getFileUploadUrl(file) # 获取文件上传路径
- fileStreamUpload(file.getBinFile(), fileUploadUrl) # 文件流上传
- keywordsPositions() # 关键字查询
|