Ver código fonte

国内对象存储兼容华为云

locky 11 meses atrás
pai
commit
7c3ae8669c

+ 6 - 0
AnsjerPush/config.py

@@ -22,6 +22,12 @@ CONFIG_EUR = 'eur'
 SERVER_TYPE = os.environ.get('DJANGO_SETTINGS_MODULE')
 SERVER_TYPE = os.environ.get('DJANGO_SETTINGS_MODULE')
 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
 
+# 华为云配置
+HUAWEICLOUD_AK = 'TN9T7ZPN3QRBBQ9NQHNB'
+HUAWEICLOUD_SK = 'rIlTBJ85MUC1WNLyJBZM2077HTsQ0qJaJf4IpTjU'
+HUAWEICLOUD_OBS_SERVER = 'https://obs.cn-east-3.myhuaweicloud.com'
+HUAWEICLOUD_PUSH_BUKET = 'asj-push'
+
 # 阿里云发邮箱
 # 阿里云发邮箱
 ALY_SES_ACCESS_NAME = 'message@dvema.com'
 ALY_SES_ACCESS_NAME = 'message@dvema.com'
 ALY_SES_ACCESS_PAW = 'SMtp123456'
 ALY_SES_ACCESS_PAW = 'SMtp123456'

+ 15 - 5
Controller/DetectControllerV2.py

@@ -5,8 +5,7 @@ import threading
 from django.http import JsonResponse
 from django.http import JsonResponse
 from django.views.generic.base import View
 from django.views.generic.base import View
 
 
-from AnsjerPush.config import CONFIG_EUR, CONFIG_INFO, CONFIG_US, CONFIG_CN
-from Object.GlobalThreadPoolObject import GlobalThreadPool
+from AnsjerPush.config import CONFIG_EUR, CONFIG_INFO, CONFIG_US
 from Object.RedisObject import RedisObject
 from Object.RedisObject import RedisObject
 from Service.DevicePushService import DevicePushService
 from Service.DevicePushService import DevicePushService
 
 
@@ -53,6 +52,7 @@ class NotificationV2View(View):
         dealings_type = int(request_dict.get('dealingsType', 0))
         dealings_type = int(request_dict.get('dealingsType', 0))
         detection = int(request_dict.get('detection', 0))
         detection = int(request_dict.get('detection', 0))
         button = request_dict.get('button', '1')
         button = request_dict.get('button', '1')
+        storage_location = request_dict.get('storage_location', None)
         uid = ""
         uid = ""
         # 参数校验
         # 参数校验
         if not all([channel, n_time]):
         if not all([channel, n_time]):
@@ -122,12 +122,22 @@ class NotificationV2View(View):
                 'event_type': event_type,
                 'event_type': event_type,
                 'n_time': n_time,
                 '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
+            # 对象存储区域, 测试/国内: 旧:aws(2),新:华为云(5),设备传参, 美洲: oci凤凰城(3), 欧洲: oci伦敦(4)
+            if CONFIG_INFO == CONFIG_US:
+                storage_location = 3
+            elif CONFIG_INFO == CONFIG_EUR:
+                storage_location = 4
+            else:
+                if storage_location is not None:
+                    storage_location = int(storage_location)
+                else:
+                    storage_location = 2
+
             params = {'nickname': nickname, 'uid': uid, 'push_kwargs': push_kwargs, 'is_st': is_st, 'region': region,
             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,
                       '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,
                       'electricity': electricity, 'bucket': bucket, 'aws_s3_client': aws_s3_client,
-                      'app_push': cache_app_push, 'storage_location': storage_location, '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,
                       'dealings_type': dealings_type, 'detection': detection,
                       'app_push_config': uid_set_push_list[0]['uid_set__msg_notify'],
                       'app_push_config': uid_set_push_list[0]['uid_set__msg_notify'],
                       'uid_set_push_list': uid_set_push_list, 'redis_obj': redis_obj}
                       'uid_set_push_list': uid_set_push_list, 'redis_obj': redis_obj}

+ 39 - 16
Service/DevicePushService.py

@@ -15,17 +15,19 @@ import time
 
 
 import boto3
 import boto3
 import botocore
 import botocore
+import oss2
 import requests
 import requests
+from obs import ObsClient
 
 
 from AnsjerPush.Config.aiConfig import DEVICE_EVENT_TYPE, ALGORITHM_COMBO_TYPES
 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, \
 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
+    AWS_SECRET_ACCESS_KEY, EVENT_DICT, EVENT_DICT_CN, CONFIG_TEST, HUAWEICLOUD_AK, HUAWEICLOUD_SK, \
+    HUAWEICLOUD_OBS_SERVER, HUAWEICLOUD_PUSH_BUKET, OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET
 from AnsjerPush.config import XMPUSH_CONFIG, OPPOPUSH_CONFIG, XM_PUSH_CHANNEL_ID
 from AnsjerPush.config import XMPUSH_CONFIG, OPPOPUSH_CONFIG, XM_PUSH_CHANNEL_ID
 from Model.models import UidPushModel, SysMsgModel, DeviceSharePermission, DeviceChannelUserSet, \
 from Model.models import UidPushModel, SysMsgModel, DeviceSharePermission, DeviceChannelUserSet, \
     DeviceChannelUserPermission, UidSetModel, Device_Info, UserAudioVideoPush, PushLog
     DeviceChannelUserPermission, UidSetModel, Device_Info, UserAudioVideoPush, PushLog
 from Object.ETkObject import ETkObject
 from Object.ETkObject import ETkObject
 from Object.OCIObjectStorage import OCIObjectStorage
 from Object.OCIObjectStorage import OCIObjectStorage
-from Object.RedisObject import RedisObject
 from Object.UidTokenObject import UidTokenObject
 from Object.UidTokenObject import UidTokenObject
 from Object.utils import LocalDateTimeUtil
 from Object.utils import LocalDateTimeUtil
 from Service.CommonService import CommonService
 from Service.CommonService import CommonService
@@ -33,7 +35,7 @@ from Service.EquipmentInfoService import EquipmentInfoService, EQUIPMENT_INFO_DI
 from Service.HuaweiPushService.HuaweiPushService import HuaweiPushObject
 from Service.HuaweiPushService.HuaweiPushService import HuaweiPushObject
 from Service.PushService import PushObject
 from Service.PushService import PushObject
 from django.db import close_old_connections
 from django.db import close_old_connections
-from Object.GlobalThreadPoolObject import GlobalThreadPool
+
 
 
 LOGGING = logging.getLogger('info')
 LOGGING = logging.getLogger('info')
 TIME_LOGGER = logging.getLogger('time')
 TIME_LOGGER = logging.getLogger('time')
@@ -733,8 +735,15 @@ class DevicePushService:
         异步推送图片
         异步推送图片
         """
         """
         try:
         try:
-            if storage_reg in [3, 4]:
+            if storage_reg == 1:
+                # 阿里云
+                auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
+                oss_img_bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
+                image_url = oss_img_bucket.sign_url('GET', key, 300)
+            elif storage_reg in [3, 4]:
                 image_url = DevicePushService.oci_object_url(uid, redis_obj, storage_reg, bucket, key)
                 image_url = DevicePushService.oci_object_url(uid, redis_obj, storage_reg, bucket, key)
+            elif storage_reg == 5:
+                image_url = DevicePushService.create_obs_signed_url(key, 'GET')
             else:
             else:
                 image_url = aws_s3_client.generate_presigned_url(
                 image_url = aws_s3_client.generate_presigned_url(
                     'get_object', Params={'Bucket': bucket, 'Key': key}, ExpiresIn=3600)
                     'get_object', Params={'Bucket': bucket, 'Key': key}, ExpiresIn=3600)
@@ -805,6 +814,7 @@ class DevicePushService:
         """
         """
         res_data = {'code': 0, 'msg': 'success'}
         res_data = {'code': 0, 'msg': 'success'}
         is_st = kwargs['is_st']
         is_st = kwargs['is_st']
+        storage_location = kwargs['storage_location']
         if is_st == 0 or is_st == 2:
         if is_st == 0 or is_st == 2:
             res_data['msg'] = 'success 0 or 2'
             res_data['msg'] = 'success 0 or 2'
         elif is_st == 1:
         elif is_st == 1:
@@ -815,15 +825,14 @@ class DevicePushService:
             else:  # 1:国外
             else:  # 1:国外
                 params['Bucket'] = 'foreignpush'
                 params['Bucket'] = 'foreignpush'
             # 区域是国外 并且存储位置OCI
             # 区域是国外 并且存储位置OCI
-            if kwargs['region'] == 1 and kwargs['storage_location'] in [3, 4]:
+            if kwargs['region'] == 1 and storage_location in [3, 4]:
                 # OCI
                 # OCI
-                img_url = DevicePushService.create_oci_req_url(kwargs['storage_location'], params['Bucket'],
-                                                               key_name)
+                img_url = DevicePushService.create_oci_req_url(storage_location, params['Bucket'], key_name)
                 res_data['img_push'] = img_url
                 res_data['img_push'] = img_url
                 res_data['msg'] = 'success 1'
                 res_data['msg'] = 'success 1'
             else:
             else:
-                # AWS
-                img_url = DevicePushService.generate_s3_url(kwargs['aws_s3_client'], params)
+                # 华为云
+                img_url = DevicePushService.create_obs_signed_url(key_name, 'PUT')
                 res_data['img_push'] = img_url
                 res_data['img_push'] = img_url
                 res_data['msg'] = 'success 1'
                 res_data['msg'] = 'success 1'
         elif is_st == 3:
         elif is_st == 3:
@@ -833,20 +842,20 @@ class DevicePushService:
             else:  # 1:国外
             else:  # 1:国外
                 params = {'Bucket': 'foreignpush'}
                 params = {'Bucket': 'foreignpush'}
             oci_client = None
             oci_client = None
-            if kwargs['storage_location'] in [3, 4]:  # 三张图的时候提前获取实例化OCI
-                region = 'eur' if kwargs['storage_location'] == 4 else 'us'
+            if storage_location in [3, 4]:  # 三张图的时候提前获取实例化OCI
+                region = 'eur' if storage_location == 4 else 'us'
                 oci_client = OCIObjectStorage(region)
                 oci_client = OCIObjectStorage(region)
             for i in range(kwargs['is_st']):
             for i in range(kwargs['is_st']):
                 key_name = '{}/{}/{}_{}.jpeg'.format(kwargs['uid'], kwargs['channel'], kwargs['n_time'], i)
                 key_name = '{}/{}/{}_{}.jpeg'.format(kwargs['uid'], kwargs['channel'], kwargs['n_time'], i)
                 params['Key'] = key_name
                 params['Key'] = key_name
-                if kwargs['region'] == 1 and kwargs['storage_location'] in [3, 4]:
+                if kwargs['region'] == 1:
                     # OCI
                     # OCI
-                    img_url = DevicePushService.create_oci_req_url(kwargs['storage_location'],
-                                                                   params['Bucket'], key_name, oci_client)
+                    img_url = DevicePushService.create_oci_req_url(storage_location, params['Bucket'], key_name,
+                                                                   oci_client)
                     img_url_list.append(img_url)
                     img_url_list.append(img_url)
                 else:
                 else:
-                    # AWS
-                    img_url = DevicePushService.generate_s3_url(kwargs['aws_s3_client'], params)
+                    # 华为云
+                    img_url = DevicePushService.create_obs_signed_url(key_name, 'PUT')
                     img_url_list.append(img_url)
                     img_url_list.append(img_url)
             res_data['img_url_list'] = img_url_list
             res_data['img_url_list'] = img_url_list
             res_data['msg'] = 'success 3'
             res_data['msg'] = 'success 3'
@@ -864,6 +873,20 @@ class DevicePushService:
         )
         )
         return response_url
         return response_url
 
 
+    @staticmethod
+    def create_obs_signed_url(key_name, method):
+        """
+        生成对象存储预签名URL
+        @param key_name: 对象名称
+        @param method: 方法
+        @return: 预签名URL
+        """
+        obs_client = ObsClient(
+            access_key_id=HUAWEICLOUD_AK, secret_access_key=HUAWEICLOUD_SK, server=HUAWEICLOUD_OBS_SERVER)
+        res = obs_client.createSignedUrl(
+            method=method, bucketName=HUAWEICLOUD_PUSH_BUKET, objectKey=key_name, expires=3600)
+        return res.signedUrl
+
     @staticmethod
     @staticmethod
     def check_share_permission(user_id, channel, uid):
     def check_share_permission(user_id, channel, uid):
         """
         """

+ 1 - 0
requirements.txt

@@ -97,3 +97,4 @@ urllib3==1.26.0
 wcwidth==0.2.13
 wcwidth==0.2.13
 zipp==3.18.1
 zipp==3.18.1
 oci~=2.125.2
 oci~=2.125.2
+esdk-obs-python==3.24.6