# -*- 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'{}".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'{}".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'