qiniuyun.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from qiniu import Auth, put_file
  2. class Qiniu(object):
  3. def __init__(self, time_out=1000, headers=None):
  4. """
  5. :param time_out: 超时时间
  6. :param headers: 请求头
  7. """
  8. self.q = Auth('wbHIPhUGq75tabnHbIpWCVvBC7c3Yt6pGG46eBZ2', 'VDb3Fhh-6LX9djR4UiXNZx7Xwk4S79fC87Kpwn-x')
  9. self.time_out = time_out
  10. self.headers = headers
  11. def up_file(self, file_path, file_name):
  12. """
  13. 七牛云上传媒体文件
  14. :param content: 图片字节流数据
  15. :return:
  16. """
  17. try:
  18. # 上传到七牛后保存的文件名uuid4+后缀 拼接url
  19. # file_path = f"{uuid.uuid4()}.jpg"
  20. file_name = 'jianyexcx/{}'.format(file_name)
  21. token = self.q.upload_token('image', file_name, 3600) # 3600指的是token的过期时间
  22. ret, info = put_file(token, file_name, file_path)
  23. if ret:
  24. # 上传成功返回地址
  25. # print('https://dm.static.elab-plus.com/{}'.format(file_name))
  26. return ret.get('key')
  27. else:
  28. print("上传失败")
  29. return None
  30. except Exception as e:
  31. # 上传失败返回None
  32. print("上传GG", e)
  33. return None
  34. def list_dir(self, text_list, dir_path):
  35. dir_files = os.listdir(dir_path) # 得到该文件夹下所有的文件
  36. for file in dir_files:
  37. file_path = os.path.join(dir_path, file) # 路径拼接成绝对路径
  38. print(file_path)
  39. if os.path.isfile(file_path): # 如果是文件,就打印这个文件路径
  40. if file_path.endswith(".jpg"):
  41. text_list.append(file_path)
  42. if os.path.isdir(file_path): # 如果目录,就递归子目录
  43. self.list_dir(text_list, file_path)
  44. return text_list
  45. def image_house(self, file_path):
  46. header = {'image_name', 'image_url'}
  47. file_list = self.list_dir([], file_path)
  48. excel_data = []
  49. for file in file_list:
  50. (path, filename) = os.path.split(file)
  51. hosue_name = filename.split('.')[0]
  52. image_url = self.up_file(file, filename)
  53. excel_data.append([hosue_name, image_url])
  54. if __name__ == '__main__':
  55. import os
  56. from file_util import FileUtil
  57. qiniu = Qiniu()
  58. file_path = r'E:\elab\建业小程序升级\10408'
  59. files_list = []
  60. files = FileUtil.load_file(file_path)
  61. # qiniu.image_house(file_path)
  62. sql_1 = 'insert into t_house_image (house_id, image_url, image_type, status, creator, created) values({}, "{}", "{}", 1, "binren", now());'
  63. sql_2 = 'update t_house_image set image_url = "{}" where house_id = {} and image_type = "{}";'
  64. sql_3 = 'delete from t_house_image where house_id = {} and image_type = "{}";'
  65. ids = []
  66. for file in files:
  67. file_name = os.path.split(file)[1].split('.')[0]
  68. name_info = file_name.split('_')
  69. house_id = int(name_info[0])
  70. image_type = name_info[1]
  71. # if house_id not in ids:
  72. # ids.append(house_id)
  73. url = qiniu.up_file(file, file_name)
  74. print(sql_3.format(house_id, image_type))
  75. print(sql_1.format(house_id, 'https://dm.static.elab-plus.com/{}'.format(url), image_type))