Pārlūkot izejas kodu

附件oss正文附件接口调用方式回退旧版本

dzr 1 mēnesi atpakaļ
vecāks
revīzija
c06f0b3262
2 mainītis faili ar 50 papildinājumiem un 7 dzēšanām
  1. 43 2
      lzz_theme/utils/aliyun.py
  2. 7 5
      lzz_theme/utils/attachment.py

+ 43 - 2
lzz_theme/utils/aliyun.py

@@ -8,12 +8,23 @@ Created on 2024-02-26
 from functools import partial
 from io import BytesIO
 
+import oss2
 import requests
 
 JY_OSS_URL = "http://172.17.162.27:18011"
 JY_OSS_TEST_URL = "http://172.31.31.203:1111"
 
 
+# 远程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"
+}
+
+
 class AttachmentError(Exception):
 
     def __init__(self, *args, **kwargs):
@@ -107,7 +118,7 @@ class OssClient(object):
 
 class JyOssClient:
 
-    def __init__(self, domain=None, mode="test"):
+    def __init__(self, domain=None, mode=None):
         if domain is None:
             domain = JY_OSS_URL
 
@@ -202,4 +213,34 @@ class JyOssClient:
     push_oss_from_local = push_oss_from_stream = _upload
 
 
-AliYunService = JyOssClient
+class OssBucketClient:
+
+    def __init__(self):
+        key_id = oss_conf['key_id']
+        key_secret = oss_conf['key_secret']
+        endpoint = oss_conf['endpoint']
+        bucket_name = oss_conf['bucket_name']
+        auth = oss2.Auth(key_id, key_secret)
+        self._bucket = oss2.Bucket(auth, endpoint, bucket_name)
+
+    def push_oss_from_local(self, key, filename):
+        """
+        上传一个本地文件到OSS的普通文件
+
+        :param str key: 上传到OSS的文件名
+        :param str filename: 本地文件名,需要有可读权限
+        """
+        return self._bucket.put_object_from_file(key, filename)
+
+    def push_oss_from_stream(self, key, data):
+        """
+        流式上传oss
+
+        :param str key: 上传到OSS的文件名
+        :param data: 待上传的内容。
+        :type data: bytes,str或file-like object
+        """
+        return self._bucket.put_object(key, data)
+
+
+AliYunService = OssBucketClient

+ 7 - 5
lzz_theme/utils/attachment.py

@@ -17,8 +17,7 @@ import tqdm
 import urllib3
 
 from utils.tools import *
-from utils.aliyun import JyOssClient
-
+from utils.aliyun import AliYunService
 
 
 urllib3.disable_warnings()
@@ -42,7 +41,8 @@ class AttachmentDownloader:
         self.dir_name = "file"
 
         self._max_retries = max_retries
-        self._oss = JyOssClient()
+        # self._oss = JyOssClient()
+        self._oss = AliYunService()
 
     def create_file(self, filename, filetype):
         os.makedirs(self.dir_name, mode=0o777, exist_ok=True)
@@ -212,7 +212,8 @@ class AttachmentDownloader:
                 attachment["fid"] = "{}.{}".format(fid, filetype)
                 attachment["size"] = self.getsize(stream)
                 attachment["url"] = "oss"
-                self._oss.upload("file", attachment["fid"], stream)
+                # self._oss.upload("file", attachment["fid"], stream)
+                self._oss.push_oss_from_stream(attachment["fid"], stream)
             except Exception as e:
                 logger.error(
                     "[{}]上传失败,原因:{}".format(filename, type(e).__name__)
@@ -263,7 +264,8 @@ class AttachmentDownloader:
                 attachment["size"] = self.getsize(file)
                 attachment["ftype"] = filetype
                 attachment["url"] = "oss"
-                self._oss.upload("file", attachment["fid"], stream)
+                # self._oss.upload("file", attachment["fid"], stream)
+                self._oss.push_oss_from_local(attachment["fid"], file)
             except Exception as e:
                 logger.error(
                     "[{}]上传失败,原因:{}".format(filename, type(e).__name__)