Pārlūkot izejas kodu

附件直连oss更换为接口调用

dzr 1 mēnesi atpakaļ
vecāks
revīzija
94305905cf
2 mainītis faili ar 202 papildinājumiem un 38 dzēšanām
  1. 192 30
      lzz_theme/utils/aliyun.py
  2. 10 8
      lzz_theme/utils/attachment.py

+ 192 - 30
lzz_theme/utils/aliyun.py

@@ -1,43 +1,205 @@
-import oss2
+# -*- coding: utf-8 -*-
+"""
+Created on 2024-02-26
+---------
+@summary: oss附件服务
+---------
+"""
+from functools import partial
+from io import BytesIO
 
 
+import requests
 
 
-# 远程bucket配置
-oss_conf = {
-    "key_id": "LTAI4G5x9aoZx8dDamQ7vfZi",
-    "key_secret": "Bk98FsbPYXcJe72n1bG3Ssf73acuNh",
-    "endpoint": "oss-cn-beijing-internal.aliyuncs.com",
-    # "endpoint": "oss-cn-beijing.aliyuncs.com",
-    "bucket_name": "jy-datafile"
-}
+JY_OSS_URL = "http://172.17.162.27:18011"
+JY_OSS_TEST_URL = "http://172.31.31.203:1111"
 
 
 
 
-class AliYunService:
+class AttachmentError(Exception):
 
 
-    def __init__(self):
-        self.__acc_key_id = oss_conf['key_id']
-        self.__acc_key_secret = oss_conf['key_secret']
-        self.__endpoint = oss_conf['endpoint']
-        self.__bucket_name = oss_conf['bucket_name']
+    def __init__(self, *args, **kwargs):
+        if 'code' not in kwargs and 'reason' not in kwargs:
+            kwargs['code'] = 0
+            kwargs['reason'] = '附件错误'
 
 
-    def push_oss_from_local(self, key, filename):
+        if 'reason' in kwargs and kwargs['reason'] is None:
+            kwargs['reason'] = '附件错误'
+
+        for key, val in kwargs.items():
+            setattr(self, key, val)
+
+        super(AttachmentError, self).__init__(*args, kwargs)
+
+
+class OssClient(object):
+    def __init__(self, domain: str):
+        # 初始化函数,用于创建类的实例
+        self.domain = domain
+
+    def upload(self, args: dict) -> dict:
+        reply = {"error_code": -1}
+        try:
+            files = {
+                'file': (args['object_name'], BytesIO(args['stream'])),
+            }
+            data = {
+                'bucket_id': args['bucket_id'],
+                'object_name': args['object_name'],
+                'gzip': args.get('gzip', False),
+            }
+
+            response = requests.post(
+                f"{self.domain}/ossservice/upload",
+                files=files,
+                data=data,
+                timeout=300,
+            )
+            if response.status_code == 200:
+                reply.update(response.json())
+            else:
+                reply['error_msg'] = f"HTTP error: {response.status_code}"
+
+        except Exception as e:
+            reply['error_msg'] = str(e)
+
+        return reply
+
+    def download(self, args: dict):
+        reply = {}
+        try:
+            data = {
+                "bucket_id": args["bucket_id"],
+                "object_name": args["object_name"]
+            }
+
+            url = f"{self.domain}/ossservice/download"
+            response = requests.post(url, data=data, timeout=300)
+            response.raise_for_status()
+
+            reply["error_code"] = 0
+            reply["error_msg"] = "下载成功"
+            reply["data"] = response.content
+
+        except Exception as e:
+            reply["error_code"] = -1
+            reply["error_msg"] = str(e)
+
+        return reply
+
+    def delete(self, args: dict):
+        reply = {}
+        try:
+            data = {
+                "bucket_id": args["bucket_id"],
+                "object_name": args["object_name"]
+            }
+
+            url = f"{self.domain}/ossservice/delete"
+            response = requests.post(url, data=data, timeout=10)
+            response.raise_for_status()
+
+            reply = response.json()
+            reply["error_code"] = 0
+        except Exception as e:
+            reply["error_code"] = -1
+            reply["error_msg"] = str(e)
+        return reply
+
+
+class JyOssClient:
+
+    def __init__(self, domain=None, mode="test"):
+        if domain is None:
+            domain = JY_OSS_URL
+
+        if mode == "test":
+            domain = JY_OSS_TEST_URL
+
+        self._oss_client = OssClient(domain=domain)
+
+    def upload(self, bucket_id, object_name, stream, gzip=False, retries=3, err_show=True):
+        """
+        上传附件
+
+        :param str bucket_id: 文件名
+        :param str object_name: 对象名称
+        :param bytes stream: 文件流
+        :param bool gzip: 是否压缩
+        :param int retries: 上传最大重试次数
+        :param bool err_show: 是否展示错误
+
+        """
+        args = {
+            "bucket_id": bucket_id,
+            "object_name": object_name,
+            "gzip": gzip,
+            "stream": stream
+        }
+
+        ret = {"error_msg": "附件上传错误", "error_code": -1}
+        for _ in range(retries):
+            ret = self._oss_client.upload(args)
+            if ret["error_code"] == 0:
+                return ret
+
+        if err_show:
+            raise AttachmentError(reason=ret.get("error_msg") or "附件上传错误")
+
+        return ret
+
+    def download(self, bucket_id, object_name, retries=3, err_show=False):
         """
         """
-        上传一个本地文件到OSS的普通文件
+        下载附件
+
+        :param str bucket_id: 文件名
+        :param str object_name: 对象名称
+        :param int retries: 下载最大重试次数
+        :param bool err_show: 是否展示错误
 
 
-        :param str key: 上传到OSS的文件名
-        :param str filename: 本地文件名,需要有可读权限
         """
         """
-        auth = oss2.Auth(self.__acc_key_id, self.__acc_key_secret)
-        bucket = oss2.Bucket(auth, self.__endpoint, self.__bucket_name)
-        bucket.put_object_from_file(key, filename)
+        args = {
+            "bucket_id": bucket_id,
+            "object_name": object_name,
+        }
 
 
-    def push_oss_from_stream(self, key, data):
+        ret = {"error_msg": "附件下载失败", "error_code": -1}
+        for _ in range(retries):
+            ret = self._oss_client.download(args)
+            if ret["error_code"] == 0 or ret["error_code"] == -1:
+                return ret
+
+        if err_show:
+            raise AttachmentError(reason=ret.get("error_msg") or "附件下载失败")
+
+        return ret
+
+    def delete(self, bucket_id, object_name, retries=3, err_show=False):
         """
         """
-        流式上传oss
+        删除附件
+
+        :param str bucket_id: 文件名
+        :param str object_name: 对象名称
+        :param int retries: 删除最大重试次数
+        :param bool err_show: 是否展示错误
 
 
-        :param str key: 上传到OSS的文件名
-        :param data: 待上传的内容。
-        :type data: bytes,str或file-like object
         """
         """
-        auth = oss2.Auth(self.__acc_key_id, self.__acc_key_secret)
-        bucket = oss2.Bucket(auth, self.__endpoint, self.__bucket_name)
-        bucket.put_object(key, data)
+        args = {
+            "bucket_id": bucket_id,
+            "object_name": object_name,
+        }
+
+        ret = {"error_msg": "附件删除失败", "error_code": -1}
+        for _ in range(retries):
+            ret = self._oss_client.delete(args)
+            if ret["error_code"] == 0:
+                return ret
+
+        if err_show:
+            raise AttachmentError(reason=ret.get("error_msg") or "附件删除失败")
+
+        return ret
+
+    _upload = partial(upload, "file")
+    push_oss_from_local = push_oss_from_stream = _upload
+
+
+AliYunService = JyOssClient

+ 10 - 8
lzz_theme/utils/attachment.py

@@ -6,8 +6,9 @@ Created on 2024-02-26
 ---------
 ---------
 @author: Dzr
 @author: Dzr
 """
 """
-import sys
 import os
 import os
+import sys
+
 sys.path.append(os.path.dirname(os.getcwd()))
 sys.path.append(os.path.dirname(os.getcwd()))
 import io
 import io
 import uuid
 import uuid
@@ -16,7 +17,7 @@ import tqdm
 import urllib3
 import urllib3
 
 
 from utils.tools import *
 from utils.tools import *
-from utils.aliyun import AliYunService
+from utils.aliyun import JyOssClient
 
 
 
 
 
 
@@ -41,6 +42,7 @@ class AttachmentDownloader:
         self.dir_name = "file"
         self.dir_name = "file"
 
 
         self._max_retries = max_retries
         self._max_retries = max_retries
+        self._oss = JyOssClient()
 
 
     def create_file(self, filename, filetype):
     def create_file(self, filename, filetype):
         os.makedirs(self.dir_name, mode=0o777, exist_ok=True)
         os.makedirs(self.dir_name, mode=0o777, exist_ok=True)
@@ -210,15 +212,15 @@ class AttachmentDownloader:
                 attachment["fid"] = "{}.{}".format(fid, filetype)
                 attachment["fid"] = "{}.{}".format(fid, filetype)
                 attachment["size"] = self.getsize(stream)
                 attachment["size"] = self.getsize(stream)
                 attachment["url"] = "oss"
                 attachment["url"] = "oss"
-                AliYunService().push_oss_from_stream(attachment["fid"], stream)
+                self._oss.upload("file", attachment["fid"], stream)
             except Exception as e:
             except Exception as e:
                 logger.error(
                 logger.error(
-                    "[{}]上传失败,原因:{}".format(filename, e.__class__.__name__)
+                    "[{}]上传失败,原因:{}".format(filename, type(e).__name__)
                 )
                 )
 
 
         return attachment
         return attachment
 
 
-    def read_pdf_in_chunks(self, pdf_path, chunk_size=1024):
+    def read_pdf_by_chunks(self, pdf_path, chunk_size=1024):
         try:
         try:
             with open(pdf_path, 'rb') as file:
             with open(pdf_path, 'rb') as file:
                 chunk = file.read(chunk_size)
                 chunk = file.read(chunk_size)
@@ -250,7 +252,7 @@ class AttachmentDownloader:
         }
         }
 
 
         if kwargs.get('is_check', None):
         if kwargs.get('is_check', None):
-            if not self.read_pdf_in_chunks(file):
+            if not self.read_pdf_by_chunks(file):
                 self.remove(file)
                 self.remove(file)
                 return attachment
                 return attachment
 
 
@@ -261,10 +263,10 @@ class AttachmentDownloader:
                 attachment["size"] = self.getsize(file)
                 attachment["size"] = self.getsize(file)
                 attachment["ftype"] = filetype
                 attachment["ftype"] = filetype
                 attachment["url"] = "oss"
                 attachment["url"] = "oss"
-                AliYunService().push_oss_from_local(attachment["fid"], file)
+                self._oss.upload("file", attachment["fid"], stream)
             except Exception as e:
             except Exception as e:
                 logger.error(
                 logger.error(
-                    "[{}]上传失败,原因:{}".format(filename, e.__class__.__name__)
+                    "[{}]上传失败,原因:{}".format(filename, type(e).__name__)
                 )
                 )
 
 
         self.remove(file)  # 删除本地临时文件
         self.remove(file)  # 删除本地临时文件