import requests import base64 import hashlib import os import esigntool from esigntool import esign_run_print_outer, fileHelp # 印章服务API config = esigntool.config() # 初始化配置类 @esign_run_print_outer def image_upload(): """ 上传印章图片 :return: """ contentType = "application/octet-stream" body = { "contentMd5": file.contentMd5, "contentType": contentType, "fileName": file.fileName, "fileSize": file.fileSize } api_path = "/v3/files/file-key" # 拼接请求路径 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) # 发送请求 print(resp.text) fileUploadUrl = resp.json()['data']['fileUploadUrl'] # 获取文件上传路径 fileKey = resp.json()['data']['fileKey'] # 获取文件上传路径 print("fileKey,请保存本地,用于后续获取图片:", fileKey) return fileUploadUrl @esign_run_print_outer def create_image(fileUploadUrl, binfile): """ 创建个人图片印章 :return: """ # 构建请求body体 contentMd5 = file.contentMd5 # 声明请求变量 contentType = "application/octet-stream" # 声明请求变量 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 if __name__ == '__main__': fileUrl = "D:\\1.jpg" file = fileHelp(fileUrl) # 初始化文件辅助类 fileUploadUrl = image_upload() # 获取文件流上传地址 create_image(fileUploadUrl, file.getBinFile()) # 上传文件流