123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- # -*- coding=utf-8
- # appid 已在配置中移除,请在参数 Bucket 中带上 appid。Bucket 由 BucketName-APPID 组成
- # 1. 设置用户配置, 包括 secretId,secretKey 以及 Region
- import os
- import re
- import time
- import datetime
- import random
- import hashlib
- from qcloud_cos import CosConfig
- from qcloud_cos import CosS3Client
- from qcloud_cos import CosServiceError
- from qcloud_cos import CosClientError
- import sys
- import logging
- # 日志
- image_upload_log = './logs/image_log2.txt' # 图片上传日志
- logging.basicConfig(level=logging.INFO,
- # stream=sys.stdout,
- filename = image_upload_log)
- # 腾讯云文件存储,存储桶,测试环境用户配置
- bucket = 'zxhx-pro-1302712961' # 桶名称
- APPID = '1302712961'
- secret_id = "AKIDC9pETRbZfWBbmhoglkT4PUJGzRjmj3Ia" # "云 API 密钥 SecretId";
- secret_key = "C6jlX4LKfleGdmfQvGNgj74lESRpBIEJ" # "云 API 密钥 SecretKey";
- TIMEOUT = 30
- token = None # 使用临时密钥需要传入Token,默认为空,可不填
- # region = "sh" # 设置一个默认的存储桶地域
- region = "ap-beijing" # ap-beijing
- cos_config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
- client = CosS3Client(cos_config) # 获取客户端对象
- # 创建存储桶
- # response = client.create_bucket(
- # Bucket=bucket
- # )
- def upload_img_to_qcloud(param_qcloud):
- put_key, localfile = param_qcloud
- # 高级上传接口(推荐)
- # 根据文件大小自动选择简单上传或分块上传,分块上传具备断点续传功能。
- response = client.upload_file(
- Bucket=bucket,
- LocalFilePath=localfile, # 本地文件的路径
- Key=put_key, # 上传到桶之后的文件名
- # PartSize=10,
- PartSize=1, # 上传分成几部分
- MAXThread=10, # 支持最多的线程数
- EnableMD5=False # 是否支持MD5
- )
- # print(response['ETag'])
- def upload_replace_image(filename_root, sid, html):
- return_error = {"errcode": 1,
- "errmsg": "word图片上传失败。"}
- daytime = datetime.datetime.now().strftime('/%Y/%m/%d/')
- image_path = filename_root + "/files"
- # todo 判断试卷是否含有图片,如果有就替换上传,没有就不处理
- local2online_dict = {}
- judge_file = os.path.isdir(image_path)
- if judge_file: # 可以不用判断
- image_number = re.findall(r'<img\s*src\s*=\s*"files/image', str(html))
- local_images_path_list = os.listdir(image_path) # 本地图片文件名
- local_images_path_list = list(filter(
- lambda x: str(x).endswith(".png") or str(x).endswith(".gif") or str(x).endswith(".jpeg") or str(x).endswith(
- ".jpg"), local_images_path_list)) # 只允许特定后缀名的图片
- if len(image_number) != len(local_images_path_list):
- return return_error
- else:
- # 從大到小把圖片进行排序
- try:
- local_images_path_list.sort(key=lambda x: int(re.search(r"image(\d+)\.[pngifje]+", str(x)).group(1)))
- except:
- return return_error
- # logger.info("local_images_path_list==>{}".format(str(local_images_path_list)))
- put_key_list = [] # 线上图片地址
- localfile_list = [] # 本地图片地址
- rawpic_list = []
- online_image_url_list = []
- if local_images_path_list:
- try:
- for i, img in enumerate(local_images_path_list, start=1): # 所有的图片【image1.png,image2.png,。。。】
- src_pat2 = re.compile(r'<img\s*src\s*=\s*"files/image{}\.png"'.format(i))
- src_pat3 = re.compile(r'<img\s*src\s*=\s*"files/image{}\.gif"'.format(i))
- src_pat4 = re.compile(r'<img\s*src\s*=\s*"files/image{}\.jpeg"'.format(i))
- src_pat5 = re.compile(r'<img\s*src\s*=\s*"files/image{}\.jpg"'.format(i))
- if str(img).endswith("png"):
- pat = src_pat2
- # re_list.append(src_pat2)
- elif str(img).endswith("gif"):
- pat = src_pat3
- # re_list.append(src_pat3)
- elif str(img).endswith("jpeg"):
- pat = src_pat4
- # re_list.append(src_pat4)
- elif str(img).endswith("jpg"):
- pat = src_pat5
- # re_list.append(src_pat5)
- # todo 上传线上,并替换线上图片
- localfile = image_path + "/{}".format(img)
- rawfile = '<img src="files'+ "/{}".format(img)
- localfile_list.append(localfile)
- rawpic_list.append(rawfile)
- # todo 图片压缩
- # resize_img(localfile, localfile)
- hash_img = get_md5(img)
- # 上传文件在空间中的名称
- put_key = "teacher/uploadfiles/wording/" + str(sid) + str(daytime) + str(hash_img)
- put_key_list.append(put_key)
- # html替换为线上的地址
- # online_image_url = "http://" + str(public_bucket) + str(addr) + "/" + str(put_key)
- online_image_url = "http://" + str(bucket) + ".cos." + region + ".myqcloud.com/" + str(put_key)
- # html = pat.sub(r'<img src={}'.format(online_image_url), str(html))
- online_image_url='<img src="{}'.format(online_image_url)
- online_image_url_list.append(online_image_url)
- local2online_dict = dict(zip(rawpic_list,online_image_url_list))
- return local2online_dict, put_key_list, localfile_list
- except:
- return return_error
- else:
- return return_error
- else:
- return local2online_dict, [], []
- def upload_replace_image2(filename_root, sid, html):
- """
- 图片的本地文件名整体替换
- :param filename_root:
- :param sid:
- :param html:
- :return:
- """
- return_error = {"errcode": 1,
- "errmsg": "word图片上传失败。"}
- daytime = datetime.datetime.now().strftime('/%Y/%m/%d/')
- image_path = filename_root + "/files"
- # todo 判断试卷是否含有图片,如果有就替换上传,没有就不处理
- judge_file = os.path.isdir(image_path)
- if judge_file:
- image_number = re.findall(r'<img\s*src\s*=\s*"files/image', str(html))
- local_images_path_list = os.listdir(image_path) # 本地图片文件名
- local_images_path_list = list(filter(
- lambda x: str(x).endswith(".png") or str(x).endswith(".gif") or str(x).endswith(".jpeg") or str(x).endswith(
- ".jpg"), local_images_path_list))
- if len(image_number) != len(local_images_path_list): # file文件夹中的图片数量是否与html中一致
- return return_error
- else:
- # 從大到小把圖片进行排序
- try:
- local_images_path_list.sort(key=lambda x: int(re.search(r"image(\d+)\.[pngifje]+", str(x)).group(1)))
- except:
- return return_error
- # logger.info("local_images_path_list==>{}".format(str(local_images_path_list)))
- put_key_list = []
- localfile_list = []
- if local_images_path_list:
- try:
- for i, img in enumerate(local_images_path_list, start=1): # 所有的图片【image1.png,image2.png,。。。】
- src_pat2 = re.compile(r'<img\s*src\s*=\s*"files/image{}\.png"'.format(i))
- src_pat3 = re.compile(r'<img\s*src\s*=\s*"files/image{}\.gif"'.format(i))
- src_pat4 = re.compile(r'<img\s*src\s*=\s*"files/image{}\.jpeg"'.format(i))
- src_pat5 = re.compile(r'<img\s*src\s*=\s*"files/image{}\.jpg"'.format(i))
- if str(img).endswith("png"):
- pat = src_pat2
- # re_list.append(src_pat2)
- elif str(img).endswith("gif"):
- pat = src_pat3
- # re_list.append(src_pat3)
- elif str(img).endswith("jpeg"):
- pat = src_pat4
- # re_list.append(src_pat4)
- elif str(img).endswith("jpg"):
- pat = src_pat5
- # re_list.append(src_pat5)
- # todo 上传线上,并替换线上图片
- localfile = image_path + "/{}".format(img)
- localfile_list.append(localfile)
- # todo 图片压缩
- # resize_img(localfile, localfile)
- hash_img = get_md5(img)
- # 上传文件在空间中的名称
- put_key = "teacher/uploadfiles/wording/" + str(sid) + str(daytime) + str(hash_img)
- put_key_list.append(put_key)
- # html替换为线上的地址
- online_image_url = "http://" + str(bucket) + ".cos." + region +".myqcloud.com/" + str(put_key)
- html = pat.sub(r'<img src={}'.format(online_image_url), str(html))
- return html, put_key_list, localfile_list
- except:
- return return_error
- else:
- return return_error
- else:
- return html, [], []
- def get_md5(image):
- """
- 由于hash不处理unicode编码的字符串(python3默认字符串是unicode)
- 所以这里判断是否字符串,如果是则进行转码
- 初始化md5、将image_name进行加密、然后返回加密字串
- """
- image_name, image_type = str(image).split(".")
- image_name = str(image_name) + str(time.time()) + str(random.random())
- if isinstance(image_name, str):
- image_name = image_name.encode("utf-8")
- md = hashlib.md5()
- md.update(image_name)
- # a = time.time()
- # b = random.random()
- return str(md.hexdigest()) + "." + str(image_type)
- if __name__ == "__main__":
- localfile1 = r"D:\zwj\word_uploads\5820\files\image1.png"
- put_key2 = "teacher/uploadfiles/wording/320/2021/01/28/573c04ef54beb46f14b4aa1d0f3ghiho.png"
- # pic_name = "99.png"
- # put_key1 = "https://" + str(bucket) + ".cos." +region+ ".myqcloud.com/" + pic_name
- # put_key2 = "http://zxhx-pro-1302712961.cos.ap-beijing.myqcloud.com/" + pic_name
- response = client.upload_file(
- Bucket=bucket,
- LocalFilePath=localfile1, # 本地文件的路径
- Key=put_key2, # 上传到桶之后的文件名
- PartSize=1,
- MAXThread=10,
- EnableMD5=False
- )
- print(response)
- print("http://{0}.cos.{1}.myqcloud.com/{2}".format(bucket, region, put_key2))
- # print(response['ETag']) # 可以与Web资源关联的记号
- #### 字节流简单上传
- # response = client.put_object(
- # Bucket=bucket,
- # Body=b'bytes',
- # Key='picture.jpg',
- # EnableMD5=False
- # )
- # print(response['ETag'])
|