소스 검색

优化异步APP消息带图链接

zhangdongming 1 년 전
부모
커밋
ff1f943b21
2개의 변경된 파일29개의 추가작업 그리고 12개의 파일을 삭제
  1. 3 2
      Object/OCIObjectStorage.py
  2. 26 10
      Service/DevicePushService.py

+ 3 - 2
Object/OCIObjectStorage.py

@@ -59,12 +59,13 @@ class OCIObjectStorage:
                          .format(errLine=e.__traceback__.tb_lineno, errMsg=repr(e)))
             return None
 
-    def get_preauthenticated_request_url(self, bucket_name, name, object_name, time_expires):
+    def get_preauthenticated_request_url(self, bucket_name, name, object_name, time_expires, access_type='ObjectRead'):
         """
         获取指定对象预认证请求URL。
         @param bucket_name: 存储桶名称
         @param name: 请求名称 是创建的预授权链接的名称,是方便管理用的,不会影响功能。比如对每个桶分别创建链接,如果要删除或者查看,可以根据name看出来是对哪个桶的链接。
         @param object_name: 对象名
+        @param access_type: 类型
         @param time_expires: 失效时间 需要datetime类型格式 例如:datetime.utcnow() + timedelta(minutes=30)
         @return: 预认证请求URL
         """
@@ -77,7 +78,7 @@ class OCIObjectStorage:
                 create_preauthenticated_request_details=oci.object_storage.models.CreatePreauthenticatedRequestDetails(
                     name=name,
                     object_name=object_name,
-                    access_type="ObjectRead",
+                    access_type=access_type,
                     time_expires=time_expires
                 )
             )

+ 26 - 10
Service/DevicePushService.py

@@ -157,7 +157,7 @@ class DevicePushService:
 
         # 低功耗产品推送,休眠702、低电量704提醒,并且detection=0,0标识单事件类型,1标识多事件类型
         is_app_push = True if params['event_type'] in [702, 704] and params['detection'] == 0 else is_app_push
-
+        redis_obj = RedisObject(3)
         # 推送
         if is_app_push:
             push_kwargs = params['push_kwargs']
@@ -195,6 +195,7 @@ class DevicePushService:
                 params['lang'] = lang
                 params['tz'] = tz
                 params['push_type'] = push_type
+                params['redis_obj'] = redis_obj
 
                 push_thread = threading.Thread(
                     target=cls.send_app_msg_push,
@@ -374,7 +375,8 @@ 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'], kwargs['storage_location']))
+                    push_kwargs['msg_title'], push_kwargs['msg_text'], kwargs['channel'], kwargs['storage_location'],
+                    kwargs['redis_obj']))
                 push_thread.start()
                 push_result = True
 
@@ -687,14 +689,14 @@ class DevicePushService:
             return response.json()
 
     @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, storage_reg):
+    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, storage_reg, redis_obj):
         """
         异步推送图片
         """
         try:
             if storage_reg in [3, 4]:
-                image_url = DevicePushService.oci_object_url(storage_reg, bucket, key)
+                image_url = DevicePushService.oci_object_url(uid, redis_obj, storage_reg, bucket, key)
             else:
                 image_url = aws_s3_client.generate_presigned_url(
                     'get_object', Params={'Bucket': bucket, 'Key': key}, ExpiresIn=3600)
@@ -716,18 +718,32 @@ class DevicePushService:
             LOGGING.error('异步推送图片异常,error_line:{},error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
     @staticmethod
-    def oci_object_url(storage_location, bucket, obj_name):
+    def oci_object_url(uid, redis_obj, storage_location, bucket, obj_name):
         """
         获取OCI对象存储URL 有效期5分钟
+        @param uid: uid
+        @param redis_obj: 缓存客户端
         @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 ''
+        try:
+            uid_key = f'PUSH:PICTURE:OCI:URL:{uid}'
+            oci_url = redis_obj.get_data(uid_key)
+            if oci_url:
+                return oci_url + obj_name
+            oci = OCIObjectStorage('eur' if storage_location == 4 else 'us')
+            prefix_name = f'{uid}/'
+            time_expires = datetime.datetime.utcnow() + datetime.timedelta(minutes=60)
+            result = oci.get_preauthenticated_request_url(bucket, 'ociPush', prefix_name, time_expires,
+                                                          'AnyObjectRead')  # 授权到指定uid文件夹
+            full_url = result.full_path if result else ''
+            redis_obj.set_data(uid_key, full_url, 3580)
+            return full_url + obj_name
+        except Exception as e:
+            LOGGING.error('oci查询消息列表异常error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+            return ''
 
     @staticmethod
     def create_oci_req_url(storage_location, bucket, obj_name, oci=None):