فهرست منبع

消息推图整合OCI对象存储

zhangdongming 1 سال پیش
والد
کامیت
c60e9abbbe
2فایلهای تغییر یافته به همراه71 افزوده شده و 17 حذف شده
  1. 6 2
      Controller/DetectControllerV2.py
  2. 65 15
      Service/DevicePushService.py

+ 6 - 2
Controller/DetectControllerV2.py

@@ -4,6 +4,7 @@ import threading
 
 from django.http import JsonResponse
 from django.views.generic.base import View
+from AnsjerPush.config import CONFIG_EUR, CONFIG_INFO, CONFIG_CN
 
 from Object.RedisObject import RedisObject
 from Service.DevicePushService import DevicePushService
@@ -117,10 +118,12 @@ class NotificationV2View(View):
                 'event_type': event_type,
                 'n_time': n_time,
             }
+            # 对象存储区域 2:AWS,3:oci美国凤凰城,4:oci英国伦敦
+            storage_location = 2 if CONFIG_INFO == CONFIG_CN else 4 if CONFIG_INFO == CONFIG_EUR else 3
             params = {'nickname': nickname, 'uid': uid, 'push_kwargs': push_kwargs, 'is_st': is_st, 'region': region,
                       'is_sys_msg': is_sys_msg, 'channel': channel, 'event_type': event_type, 'n_time': n_time,
                       'electricity': electricity, 'bucket': bucket, 'aws_s3_client': aws_s3_client,
-                      'app_push': cache_app_push, 'storage_location': 2, 'ai_type': ai_type, 'device_type': device_type,
+                      'app_push': cache_app_push, 'storage_location': storage_location, 'ai_type': ai_type, 'device_type': device_type,
                       'dealings_type': dealings_type, 'detection': detection,
                       'app_push_config': uid_set_push_list[0]['uid_set__msg_notify'],
                       'uid_set_push_list': uid_set_push_list}
@@ -143,7 +146,8 @@ class NotificationV2View(View):
                 'channel': channel,
                 'n_time': n_time,
                 'region': region,
-                'aws_s3_client': aws_s3_client
+                'aws_s3_client': aws_s3_client,
+                'storage_location': storage_location
             }
             res_data = DevicePushService.get_res_data(**kwargs)
 

+ 65 - 15
Service/DevicePushService.py

@@ -6,6 +6,7 @@
 @Email   : zhangdongming@asj6.wecom.work
 @Software: PyCharm
 """
+import datetime
 import hashlib
 import json
 import logging
@@ -19,10 +20,11 @@ import requests
 from AnsjerPush.Config.aiConfig import DEVICE_EVENT_TYPE, ALGORITHM_COMBO_TYPES
 from AnsjerPush.config import CONFIG_INFO, CONFIG_CN, MULTI_CHANNEL_TYPE_LIST, SYS_EVENT_TYPE_LIST, AWS_ACCESS_KEY_ID, \
     AWS_SECRET_ACCESS_KEY, EVENT_DICT, EVENT_DICT_CN, CONFIG_TEST
-from AnsjerPush.config import XMPUSH_CONFIG, OPPOPUSH_CONFIG
+from AnsjerPush.config import XMPUSH_CONFIG, OPPOPUSH_CONFIG, XM_PUSH_CHANNEL_ID
 from Model.models import UidPushModel, SysMsgModel, DeviceSharePermission, DeviceChannelUserSet, \
     DeviceChannelUserPermission, UidSetModel, Device_Info
 from Object.ETkObject import ETkObject
+from Object.OCIObjectStorage import OCIObjectStorage
 from Object.RedisObject import RedisObject
 from Object.UidTokenObject import UidTokenObject
 from Object.utils import LocalDateTimeUtil
@@ -95,7 +97,6 @@ class DevicePushService:
         @param event_type: 事件类型
         @return: uid_push_qs
         """
-        start_time = time.time()
         if event_type not in [606, 607]:
             uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid, uid_set__detect_status=1). \
                 values('token_val', 'app_type', 'appBundleId', 'm_code', 'push_type', 'userID_id', 'userID__NickName',
@@ -111,8 +112,6 @@ class DevicePushService:
                        'lang', 'm_code', 'tz', 'uid_set__nickname', 'uid_set__detect_interval', 'uid_set__detect_group',
                        'uid_set__channel', 'uid_set__ai_type', 'uid_set__device_type', 'uid_set__new_detect_interval',
                        'uid_set__msg_notify', 'jg_token_val')
-        end_time = time.time()
-        LOGGING.info(f'{uid}查询推送token耗时:{end_time-start_time}ms')
         return uid_push_qs
 
     @staticmethod
@@ -375,7 +374,7 @@ class DevicePushService:
                 push_thread = threading.Thread(target=cls.async_send_picture_push, args=(
                     push_type, kwargs['aws_s3_client'], kwargs['bucket'], key,
                     kwargs['uid'], kwargs['appBundleId'], kwargs['token_val'], kwargs['event_type'], kwargs['n_time'],
-                    push_kwargs['msg_title'], push_kwargs['msg_text'], kwargs['channel']))
+                    push_kwargs['msg_title'], push_kwargs['msg_text'], kwargs['channel'], kwargs['storage_location']))
                 push_thread.start()
                 push_result = True
 
@@ -424,7 +423,7 @@ class DevicePushService:
                     push_result = PushObject.android_honorpush(**push_kwargs)
             return push_result
         except Exception as e:
-            LOGGING.info('发送推送异常,error_line:{},error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+            LOGGING.error('发送推送异常,error_line:{},error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
             return False
 
     @staticmethod
@@ -689,13 +688,16 @@ class DevicePushService:
 
     @classmethod
     def async_send_picture_push(cls, push_type, aws_s3_client, bucket, key, uid, appBundleId,
-                                token_val, event_type, n_time, msg_title, msg_text, channel):
+                                token_val, event_type, n_time, msg_title, msg_text, channel, storage_reg):
         """
         异步推送图片
         """
         try:
-            image_url = aws_s3_client.generate_presigned_url(
-                'get_object', Params={'Bucket': bucket, 'Key': key}, ExpiresIn=3600)
+            if storage_reg in [3, 4]:
+                image_url = DevicePushService.oci_object_url(storage_reg, bucket, key)
+            else:
+                image_url = aws_s3_client.generate_presigned_url(
+                    'get_object', Params={'Bucket': bucket, 'Key': key}, ExpiresIn=3600)
             push_result = False
             if push_type == 0:
                 push_result = PushObject.ios_apns_push(
@@ -711,7 +713,35 @@ class DevicePushService:
 
             LOGGING.info('{}推送图片,push_type:{},推送结果:{}'.format(uid, push_type, push_result))
         except Exception as e:
-            LOGGING.info('异步推送图片异常,error_line:{},error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+            LOGGING.error('异步推送图片异常,error_line:{},error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+
+    @staticmethod
+    def oci_object_url(storage_location, bucket, obj_name):
+        """
+        获取OCI对象存储URL 有效期5分钟
+        @param storage_location: 存储区域
+        @param bucket: 存储桶
+        @param obj_name: 对象名称
+        @return: url
+        """
+        oci = OCIObjectStorage('eur' if storage_location == 4 else 'us')
+        time_expires = datetime.datetime.utcnow() + datetime.timedelta(minutes=60)
+        result = oci.get_preauthenticated_request_url(bucket, 'ociPush', obj_name, time_expires)
+        return result.full_path if result else ''
+
+    @staticmethod
+    def create_oci_req_url(storage_location, bucket, obj_name, oci=None):
+        """
+        创建oci预认证请求url
+        """
+        if not oci:
+            region = 'eur' if storage_location == 4 else 'us'
+            oci = OCIObjectStorage(region)
+        time_expires = datetime.datetime.utcnow() + datetime.timedelta(minutes=60)
+        result = oci.create_ereauthenticated_request(bucket, 'ociPush', obj_name, time_expires)
+        if not result:
+            return ''
+        return result.full_path + result.object_name
 
     @staticmethod
     def get_res_data(**kwargs):
@@ -730,20 +760,40 @@ class DevicePushService:
                 params['Bucket'] = 'push'
             else:  # 1:国外
                 params['Bucket'] = 'foreignpush'
-            img_url = DevicePushService.generate_s3_url(kwargs['aws_s3_client'], params)
-            res_data['img_push'] = img_url
-            res_data['msg'] = 'success 1'
+            # 区域是国外 并且存储位置OCI
+            if kwargs['region'] == 1 and kwargs['storage_location'] in [3, 4]:
+                # OCI
+                img_url = DevicePushService.create_oci_req_url(kwargs['storage_location'], params['Bucket'],
+                                                               key_name)
+                res_data['img_push'] = img_url
+                res_data['msg'] = 'success 1'
+            else:
+                # AWS
+                img_url = DevicePushService.generate_s3_url(kwargs['aws_s3_client'], params)
+                res_data['img_push'] = img_url
+                res_data['msg'] = 'success 1'
         elif is_st == 3:
             img_url_list = []
             if kwargs['region'] == 2:  # 2:国内
                 params = {'Bucket': 'push'}
             else:  # 1:国外
                 params = {'Bucket': 'foreignpush'}
+            oci_client = None
+            if kwargs['storage_location'] in [3, 4]:  # 三张图的时候提前获取实例化OCI
+                region = 'eur' if kwargs['storage_location'] == 4 else 'us'
+                oci_client = OCIObjectStorage(region)
             for i in range(kwargs['is_st']):
                 key_name = '{}/{}/{}_{}.jpeg'.format(kwargs['uid'], kwargs['channel'], kwargs['n_time'], i)
                 params['Key'] = key_name
-                img_url = DevicePushService.generate_s3_url(kwargs['aws_s3_client'], params)
-                img_url_list.append(img_url)
+                if kwargs['region'] == 1 and kwargs['storage_location'] in [3, 4]:
+                    # OCI
+                    img_url = DevicePushService.create_oci_req_url(kwargs['storage_location'],
+                                                                   params['Bucket'], key_name, oci_client)
+                    img_url_list.append(img_url)
+                else:
+                    # AWS
+                    img_url = DevicePushService.generate_s3_url(kwargs['aws_s3_client'], params)
+                    img_url_list.append(img_url)
             res_data['img_url_list'] = img_url_list
             res_data['msg'] = 'success 3'
         return res_data