minio.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # -*- coding:utf-8 -*-
  2. # powered by Carlos
  3. # October 23, 2019
  4. import base64
  5. import io
  6. from minio import Minio
  7. from minio.error import (ResponseError, BucketAlreadyOwnedByYou,
  8. BucketAlreadyExists, NoSuchKey)
  9. from configs.settings import config
  10. from PIL import Image
  11. from io import BytesIO
  12. URL = config.get('MINIO', 'URL')
  13. ACCESS_KEY = config.get('MINIO', 'ACCESS_KEY')
  14. SECRET_KEY = config.get('MINIO', 'SECRET_KEY')
  15. class FileHandler(object):
  16. def __init__(self, bucket_name):
  17. self.bucket_name = bucket_name
  18. self.minio_client = Minio(URL,
  19. access_key=ACCESS_KEY,
  20. secret_key=SECRET_KEY,
  21. secure=False)
  22. # 无脑创桶
  23. try:
  24. self.minio_client.make_bucket(self.bucket_name)
  25. except BucketAlreadyExists:
  26. pass
  27. except BucketAlreadyOwnedByYou:
  28. pass
  29. except ResponseError:
  30. # print('dssds')
  31. raise Exception("minio连接失败")
  32. # 上传图片
  33. def put_image(self, img_name, image):
  34. file_name = img_name
  35. file_byte = base64.b64decode(image)
  36. f = io.BytesIO(file_byte)
  37. try:
  38. self.minio_client.put_object(self.bucket_name, file_name, f, len(file_byte))
  39. return file_name
  40. except ResponseError:
  41. raise Exception("minio创建图片失败 文件名:%s" % file_name)
  42. def put_byte_file(self, file_name, file_content):
  43. f = io.BytesIO(file_content)
  44. try:
  45. self.minio_client.put_object(self.bucket_name, file_name, f, len(file_content))
  46. return file_name
  47. except ResponseError:
  48. raise Exception("minio创建文件失败 文件名:%s" % file_name)
  49. def put_np_image(self, img_name, np_image):
  50. file_name = img_name
  51. img_crop_pil = Image.fromarray(np_image)
  52. byte_io = BytesIO()
  53. img_crop_pil.save(byte_io, format="jpeg")
  54. jpg_buffer = byte_io.getvalue()
  55. byte_io = BytesIO(jpg_buffer)
  56. try:
  57. self.minio_client.put_object(self.bucket_name, img_name, byte_io, len(jpg_buffer))
  58. return file_name
  59. except ResponseError:
  60. raise Exception("minio创建图片失败 文件名:%s" % file_name)
  61. # 获取照片
  62. def get_file(self, img_name):
  63. file_name = img_name
  64. try:
  65. d = self.minio_client.get_object(self.bucket_name, file_name)
  66. return d.data
  67. except NoSuchKey:
  68. pass
  69. except Exception as e:
  70. print("Get minio pic failed:{}".format(e))
  71. return bytes()
  72. def del_file(self, file_name):
  73. try:
  74. self.minio_client.remove_object(self.bucket_name, file_name)
  75. except ResponseError:
  76. raise Exception("删除minio文件失败 文件名:%s" % file_name)
  77. def ls_file(self, filename):
  78. objects = []
  79. try:
  80. objects = self.minio_client.list_objects(self.bucket_name, prefix=filename,
  81. recursive=True)
  82. return objects
  83. except ResponseError:
  84. raise Exception("列出文件失败")
  85. minio_client = FileHandler('datax')