12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- from qiniu import Auth, put_file
- class Qiniu(object):
- def __init__(self, time_out=1000, headers=None):
- """
- :param time_out: 超时时间
- :param headers: 请求头
- """
- self.q = Auth('wbHIPhUGq75tabnHbIpWCVvBC7c3Yt6pGG46eBZ2', 'VDb3Fhh-6LX9djR4UiXNZx7Xwk4S79fC87Kpwn-x')
- self.time_out = time_out
- self.headers = headers
- def up_file(self, file_path, file_name):
- """
- 七牛云上传媒体文件
- :param content: 图片字节流数据
- :return:
- """
- try:
- # 上传到七牛后保存的文件名uuid4+后缀 拼接url
- # file_path = f"{uuid.uuid4()}.jpg"
- file_name = 'jianyexcx/{}'.format(file_name)
- token = self.q.upload_token('image', file_name, 3600) # 3600指的是token的过期时间
- ret, info = put_file(token, file_name, file_path)
- if ret:
- # 上传成功返回地址
- # print('https://dm.static.elab-plus.com/{}'.format(file_name))
- return ret.get('key')
- else:
- print("上传失败")
- return None
- except Exception as e:
- # 上传失败返回None
- print("上传GG", e)
- return None
- def list_dir(self, text_list, dir_path):
- dir_files = os.listdir(dir_path) # 得到该文件夹下所有的文件
- for file in dir_files:
- file_path = os.path.join(dir_path, file) # 路径拼接成绝对路径
- print(file_path)
- if os.path.isfile(file_path): # 如果是文件,就打印这个文件路径
- if file_path.endswith(".jpg"):
- text_list.append(file_path)
- if os.path.isdir(file_path): # 如果目录,就递归子目录
- self.list_dir(text_list, file_path)
- return text_list
- def image_house(self, file_path):
- header = {'image_name', 'image_url'}
- file_list = self.list_dir([], file_path)
- excel_data = []
- for file in file_list:
- (path, filename) = os.path.split(file)
- hosue_name = filename.split('.')[0]
- image_url = self.up_file(file, filename)
- excel_data.append([hosue_name, image_url])
- if __name__ == '__main__':
- import os
- from file_util import FileUtil
- qiniu = Qiniu()
- file_path = r'E:\elab\建业小程序升级\10408'
- files_list = []
- files = FileUtil.load_file(file_path)
- # qiniu.image_house(file_path)
- sql_1 = 'insert into t_house_image (house_id, image_url, image_type, status, creator, created) values({}, "{}", "{}", 1, "binren", now());'
- sql_2 = 'update t_house_image set image_url = "{}" where house_id = {} and image_type = "{}";'
- sql_3 = 'delete from t_house_image where house_id = {} and image_type = "{}";'
- ids = []
- for file in files:
- file_name = os.path.split(file)[1].split('.')[0]
- name_info = file_name.split('_')
- house_id = int(name_info[0])
- image_type = name_info[1]
- # if house_id not in ids:
- # ids.append(house_id)
- url = qiniu.up_file(file, file_name)
- print(sql_3.format(house_id, image_type))
- print(sql_1.format(house_id, 'https://dm.static.elab-plus.com/{}'.format(url), image_type))
|