photo_upload_qcloud.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # -*- coding=utf-8
  2. # appid 已在配置中移除,请在参数 Bucket 中带上 appid。Bucket 由 BucketName-APPID 组成
  3. # 1. 设置用户配置, 包括 secretId,secretKey 以及 Region
  4. import os
  5. import re
  6. import time
  7. import datetime
  8. import random
  9. import hashlib
  10. from qcloud_cos import CosConfig
  11. from qcloud_cos import CosS3Client
  12. from qcloud_cos import CosServiceError
  13. from qcloud_cos import CosClientError
  14. import sys
  15. import logging
  16. # 日志
  17. image_upload_log = './logs/image_log2.txt' # 图片上传日志
  18. logging.basicConfig(level=logging.INFO,
  19. # stream=sys.stdout,
  20. filename = image_upload_log)
  21. # 腾讯云文件存储,存储桶,测试环境用户配置
  22. public_bucket = 'zxhx-pro-1302712961' # 桶名称
  23. APPID = '1302712961'
  24. secret_id = "AKIDC9pETRbZfWBbmhoglkT4PUJGzRjmj3Ia" # "云 API 密钥 SecretId";
  25. secret_key = "C6jlX4LKfleGdmfQvGNgj74lESRpBIEJ" # "云 API 密钥 SecretKey";
  26. TIMEOUT = 30
  27. token = None # 使用临时密钥需要传入Token,默认为空,可不填
  28. # 设置一个默认的存储桶地域
  29. region = "ap-beijing"
  30. addr = ".cos." + region + ".myqcloud.com" # 后缀拼接
  31. scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填
  32. cos_config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
  33. client = CosS3Client(cos_config) # 获取客户端对象
  34. def upload_img_to_ucloud(param_qcloud):
  35. put_key, localfile = param_qcloud
  36. # 高级上传接口(推荐)
  37. # 根据文件大小自动选择简单上传或分块上传,分块上传具备断点续传功能。
  38. response = client.upload_file(
  39. Bucket=public_bucket,
  40. LocalFilePath=localfile, # 本地文件的路径
  41. Key=put_key, # 上传到桶之后的文件名
  42. # PartSize=10,
  43. PartSize=1, # 上传分成几部分
  44. MAXThread=10, # 支持最多的线程数
  45. EnableMD5=False # 是否支持MD5
  46. )
  47. # print(response['ETag'])
  48. def upload_replace_image(filename_root, sid, html):
  49. return_error = {"errcode": 1,
  50. "errmsg": "word图片上传失败。"}
  51. daytime = datetime.datetime.now().strftime('/%Y/%m/%d/')
  52. image_path = filename_root + "/files"
  53. # todo 判断试卷是否含有图片,如果有就替换上传,没有就不处理
  54. local2online_dict = {}
  55. judge_file = os.path.isdir(image_path)
  56. if judge_file: # 可以不用判断
  57. image_number = re.findall(r'<img\s*src\s*=\s*"files/image', str(html))
  58. local_images_path_list = os.listdir(image_path) # 本地图片文件名
  59. local_images_path_list = list(filter(
  60. lambda x: str(x).endswith(".png") or str(x).endswith(".gif") or str(x).endswith(".jpeg") or str(x).endswith(
  61. ".jpg"), local_images_path_list)) # 只允许特定后缀名的图片
  62. if len(image_number) != len(local_images_path_list):
  63. return return_error
  64. else:
  65. # 從大到小把圖片进行排序
  66. try:
  67. local_images_path_list.sort(key=lambda x: int(re.search(r"image(\d+)\.[pngifje]+", str(x)).group(1)))
  68. except:
  69. return return_error
  70. # logger.info("local_images_path_list==>{}".format(str(local_images_path_list)))
  71. put_key_list = [] # 上传云上的图片名称
  72. localfile_list = [] # 本地图片地址
  73. rawpic_list = [] # html中的图片路径
  74. online_image_url_list = [] # 线上图片地址
  75. if local_images_path_list:
  76. try:
  77. for i, img in enumerate(local_images_path_list, start=1): # 所有的图片【image1.png,image2.png,。。。】
  78. # src_pat2 = re.compile(r'<img\s*src\s*=\s*"files/image{}\.png"'.format(i))
  79. # src_pat3 = re.compile(r'<img\s*src\s*=\s*"files/image{}\.gif"'.format(i))
  80. # src_pat4 = re.compile(r'<img\s*src\s*=\s*"files/image{}\.jpeg"'.format(i))
  81. # src_pat5 = re.compile(r'<img\s*src\s*=\s*"files/image{}\.jpg"'.format(i))
  82. # if str(img).endswith("png"):
  83. # pat = src_pat2
  84. # # re_list.append(src_pat2)
  85. # elif str(img).endswith("gif"):
  86. # pat = src_pat3
  87. # # re_list.append(src_pat3)
  88. # elif str(img).endswith("jpeg"):
  89. # pat = src_pat4
  90. # # re_list.append(src_pat4)
  91. # elif str(img).endswith("jpg"):
  92. # pat = src_pat5
  93. # # re_list.append(src_pat5)
  94. # todo 上传线上,并替换线上图片
  95. localfile = image_path + "/{}".format(img)
  96. rawfile = '<img src="files' + "/{}".format(img)
  97. localfile_list.append(localfile)
  98. rawpic_list.append(rawfile)
  99. # todo 图片压缩
  100. # resize_img(localfile, localfile)
  101. hash_img = get_md5(img)
  102. # 上传文件在空间中的名称
  103. put_key = "teacher/uploadfiles/wording/" + str(sid) + str(daytime) + str(hash_img)
  104. put_key_list.append(put_key)
  105. # html替换为线上的地址
  106. online_image_url = "http://" + str(public_bucket) + str(addr) + "/" + str(put_key)
  107. # html = pat.sub(r'<img src={}'.format(online_image_url), str(html))
  108. online_image_url = '<img src="{}'.format(online_image_url)
  109. online_image_url_list.append(online_image_url)
  110. local2online_dict = dict(zip(rawpic_list, online_image_url_list))
  111. return local2online_dict, put_key_list, localfile_list
  112. except:
  113. return return_error
  114. else:
  115. return return_error
  116. else:
  117. return local2online_dict, [], []
  118. def get_md5(image):
  119. """
  120. 由于hash不处理unicode编码的字符串(python3默认字符串是unicode)
  121. 所以这里判断是否字符串,如果是则进行转码
  122. 初始化md5、将image_name进行加密、然后返回加密字串
  123. """
  124. image_name, image_type = str(image).split(".")
  125. image_name = str(image_name) + str(time.time()) + str(random.random())
  126. if isinstance(image_name, str):
  127. image_name = image_name.encode("utf-8")
  128. md = hashlib.md5()
  129. md.update(image_name)
  130. # a = time.time()
  131. # b = random.random()
  132. return str(md.hexdigest()) + "." + str(image_type)