Forráskód Böngészése

消息推送整合OCI对象存储

zhangdongming 1 éve
szülő
commit
4f08d120e9
2 módosított fájl, 64 hozzáadás és 13 törlés
  1. 6 2
      Controller/DetectControllerV2.py
  2. 58 11
      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)
 

+ 58 - 11
Service/DevicePushService.py

@@ -6,6 +6,7 @@
 @Email   : zhangdongming@asj6.wecom.work
 @Software: PyCharm
 """
+import datetime
 import hashlib
 import json
 import logging
@@ -23,6 +24,7 @@ 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
@@ -376,7 +378,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
 
@@ -426,7 +428,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
@@ -694,13 +696,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(
@@ -716,7 +721,34 @@ 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预认证请求url
+        """
+        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):
@@ -735,9 +767,17 @@ 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:国内
@@ -747,8 +787,15 @@ class DevicePushService:
             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)
+                    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