Procházet zdrojové kódy

旧推送接口图片存储兼容oci

locky před 10 měsíci
rodič
revize
90e18f7091
2 změnil soubory, kde provedl 36 přidání a 23 odebrání
  1. 25 19
      Controller/DetectController.py
  2. 11 4
      Service/DevicePushService.py

+ 25 - 19
Controller/DetectController.py

@@ -6,7 +6,7 @@ import oss2
 from django.http import JsonResponse
 from django.views.generic.base import View
 
-from AnsjerPush.config import CONFIG_INFO, CONFIG_CN
+from AnsjerPush.config import CONFIG_INFO, CONFIG_CN, CONFIG_US, CONFIG_EUR
 from AnsjerPush.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET
 from Object.RedisObject import RedisObject
 from Service.DevicePushService import DevicePushService
@@ -52,6 +52,7 @@ class NotificationView(View):
 
             V1_PUSH_LOGGER.info('旧移动侦测接口uid:{},时间戳:{},事件类型:{}'.format(uid, n_time, event_type))
 
+            is_st = int(is_st)
             event_type = int(event_type)
             pkey = '{}_{}_{}_ptl'.format(uid, event_type, channel)
             ykey = '{}_redis_qs'.format(uid)
@@ -134,24 +135,29 @@ class NotificationView(View):
                 kwargs=params)
             push_thread.start()
 
-            res_data = {}
-            if is_st == '0' or is_st == '2':
-                res_data = {'code': 0, 'msg': 'success 0 or 2'}
-                return JsonResponse(status=200, data=res_data)
-
-            elif is_st == '1':
-                obj = '{}/{}/{}.jpeg'.format(uid, channel, n_time)
-                url = bucket.sign_url('PUT', obj, 3600)
-                res_data = {'code': 0, 'img_push': url, 'msg': 'success 1'}
-
-            elif is_st == '3':
-                img_url_list = []
-                for i in range(int(is_st)):
-                    obj = '{}/{}/{}_{}.jpeg'.format(uid, channel, n_time, i)
-                    url = bucket.sign_url('PUT', obj, 3600)
-                    img_url_list.append(url)
-
-                res_data = {'code': 0, 'img_url_list': img_url_list, 'msg': 'success 3'}
+            # 对象存储区域
+            # region: 国外(1), 国内(2)
+            # 测试/国内: 阿里云(1), 美洲: oci凤凰城(3), 欧洲: oci伦敦(4)
+            region = 1
+            if CONFIG_INFO == CONFIG_US:
+                storage_location = 3
+            elif CONFIG_INFO == CONFIG_EUR:
+                storage_location = 4
+            else:
+                region = 2
+                storage_location = 1
+            # 获取图片上传链接
+            kwargs = {
+                'is_st': is_st,
+                'uid': uid,
+                'channel': channel,
+                'n_time': n_time,
+                'region': region,
+                'aws_s3_client': '',
+                'storage_location': storage_location,
+                'oss_bucket': bucket
+            }
+            res_data = DevicePushService.get_res_data(**kwargs)
 
             V1_PUSH_LOGGER.info('旧推送接口响应,uid:{},n_time:{},事件类型:{},响应:{}'.
                                 format(uid, n_time, event_type, json.dumps(res_data)))

+ 11 - 4
Service/DevicePushService.py

@@ -838,6 +838,7 @@ class DevicePushService:
         storage_location = kwargs['storage_location']
         if is_st == 0 or is_st == 2:
             res_data['msg'] = 'success 0 or 2'
+
         elif is_st == 1:
             key_name = '{}/{}/{}.jpeg'.format(kwargs['uid'], kwargs['channel'], kwargs['n_time'])
             params = {'Key': key_name}
@@ -850,16 +851,18 @@ class DevicePushService:
                 # OCI
                 img_url = DevicePushService.create_oci_req_url(storage_location, params['Bucket'], key_name)
                 res_data['img_push'] = img_url
-                res_data['msg'] = 'success 1'
             elif storage_location == 2:
                 # AWS
                 img_url = DevicePushService.generate_s3_url(kwargs['aws_s3_client'], params)
-                res_data['img_push'] = img_url
+            elif storage_location == 1:
+                # 阿里云
+                img_url = kwargs['oss_bucket'].sign_url('PUT', key_name, 3600)
             else:
                 # 华为云
                 img_url = DevicePushService.create_obs_signed_url(key_name, 'PUT')
-                res_data['img_push'] = img_url
-                res_data['msg'] = 'success 1'
+            res_data['img_push'] = img_url
+            res_data['msg'] = 'success 1'
+
         elif is_st == 3:
             img_url_list = []
             if kwargs['region'] == 2:  # 2:国内
@@ -880,12 +883,16 @@ class DevicePushService:
                 elif storage_location == 2:
                     # AWS
                     img_url = DevicePushService.generate_s3_url(kwargs['aws_s3_client'], params)
+                elif storage_location == 1:
+                    # 阿里云
+                    img_url = kwargs['oss_bucket'].sign_url('PUT', key_name, 3600)
                 else:
                     # 华为云
                     img_url = DevicePushService.create_obs_signed_url(key_name, 'PUT')
                 img_url_list.append(img_url)
             res_data['img_url_list'] = img_url_list
             res_data['msg'] = 'success 3'
+
         return res_data
 
     @staticmethod