common.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding:utf-8 -*-
  2. # @Time : 2020/10/28 2:13 下午
  3. # @Author : Creat by Han
  4. import requests
  5. import os
  6. import oss2
  7. import json
  8. from PIL.BufrStubImagePlugin import Image
  9. from Crypto.Util.py3compat import BytesIO
  10. from django.conf import settings
  11. config_dict = settings.CONFIG
  12. auth = oss2.Auth('LTAI4G7tFh5Nk4KXZoSPk1D8', 'RV4S2SfbLPoFNjlI4uIOoA0J1LQPQc')
  13. def down_image(image_name,url):
  14. dir_name = os.path.dirname(image_name)
  15. if not os.path.isdir(dir_name):
  16. os.makedirs(dir_name)
  17. content = requests.get(url).content
  18. with open("%s"%image_name,"wb") as f:
  19. f.write(content)
  20. return image_name
  21. def push_oss_image(data,http="https://oss-cn-beijing.aliyuncs.com",bt="bigdata-all"):
  22. try:
  23. http = config_dict["oss"]["http"]
  24. bt = config_dict["oss"]["bucket"]
  25. bucket = oss2.Bucket(auth,http,bt,connect_timeout=30)
  26. headers = {}
  27. headers["Content-Type"] = "image/jpg"
  28. headers["Content-Disposition"] = "inline"
  29. bucket.put_object_from_file(data[0].strip('/'), data[1],headers=headers)
  30. requests.adapters.DEFAULT_RETRIES = 5
  31. s = requests.session()
  32. s.keep_alive = False
  33. if os.path.exists(data[1]) == True:
  34. # 删除图片文件
  35. os.remove(data[1])
  36. except Exception as e:
  37. print("错误",e)
  38. pass
  39. return True
  40. if __name__ == '__main__':
  41. # url = "http://image-yunfei.oss-cn-beijing.aliyuncs.com/202010/867435052201308/2020102840.jpg"
  42. base_path = "/data/image_result/"
  43. http = "http://bigdata-image.yfpyx.com/"
  44. addr = "202010/867435052201308/20201028211.jpg"
  45. # addr = "08-202/867435052201043/08-2020.jpg"
  46. image_path = base_path + addr.split('/')[-1]
  47. url = http+addr
  48. # data = ['/202010/867435052201308/up_2020102836.jpg', 'result_2020102836.jpg']
  49. # http = "http://oss-cn-beijing.aliyuncs.com"
  50. # bt = "image-yunfei"
  51. # push_oss_image(data=data,http=http,bt=bt)