소스 검색

v2推送图片保存到华为云

locky 1 년 전
부모
커밋
86a967c83a
3개의 변경된 파일43개의 추가작업 그리고 17개의 파일을 삭제
  1. 6 0
      AnsjerPush/config.py
  2. 8 3
      Controller/DetectControllerV2.py
  3. 29 14
      Service/DevicePushService.py

+ 6 - 0
AnsjerPush/config.py

@@ -22,6 +22,12 @@ CONFIG_EUR = 'eur'
 SERVER_TYPE = os.environ.get('DJANGO_SETTINGS_MODULE')
 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_PAW = 'SMtp123456'

+ 8 - 3
Controller/DetectControllerV2.py

@@ -4,7 +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 AnsjerPush.config import CONFIG_EUR, CONFIG_INFO, CONFIG_CN, CONFIG_US
 
 from Object.RedisObject import RedisObject
 from Service.DevicePushService import DevicePushService
@@ -119,8 +119,13 @@ 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
+            # 对象存储区域, 测试/国内: 华为云, 美洲: oci凤凰城, 欧洲: oci伦敦
+            storage_location = 5
+            if CONFIG_INFO == CONFIG_US:
+                storage_location = 3
+            elif CONFIG_INFO == CONFIG_EUR:
+                storage_location = 4
+
             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,

+ 29 - 14
Service/DevicePushService.py

@@ -16,10 +16,12 @@ import time
 import boto3
 import botocore
 import requests
+from obs import ObsClient
 
 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
+    AWS_SECRET_ACCESS_KEY, EVENT_DICT, EVENT_DICT_CN, CONFIG_TEST, HUAWEICLOUD_AK, HUAWEICLOUD_SK, \
+    HUAWEICLOUD_OBS_SERVER, HUAWEICLOUD_PUSH_BUKET
 from AnsjerPush.config import XMPUSH_CONFIG, OPPOPUSH_CONFIG, XM_PUSH_CHANNEL_ID
 from Model.models import UidPushModel, SysMsgModel, DeviceSharePermission, DeviceChannelUserSet, \
     DeviceChannelUserPermission, UidSetModel, Device_Info, UserAudioVideoPush
@@ -713,6 +715,8 @@ class DevicePushService:
         try:
             if storage_reg in [3, 4]:
                 image_url = DevicePushService.oci_object_url(storage_reg, bucket, key)
+            elif storage_reg == 5:
+                image_url = DevicePushService.create_obs_signed_url(uid, channel, n_time, 'GET')
             else:
                 image_url = aws_s3_client.generate_presigned_url(
                     'get_object', Params={'Bucket': bucket, 'Key': key}, ExpiresIn=3600)
@@ -769,6 +773,7 @@ class DevicePushService:
         """
         res_data = {'code': 0, 'msg': 'success'}
         is_st = kwargs['is_st']
+        storage_location = kwargs['storage_location']
         if is_st == 0 or is_st == 2:
             res_data['msg'] = 'success 0 or 2'
         elif is_st == 1:
@@ -779,16 +784,15 @@ class DevicePushService:
             else:  # 1:国外
                 params['Bucket'] = 'foreignpush'
             # 区域是国外 并且存储位置OCI
-            if kwargs['region'] == 1 and kwargs['storage_location'] in [3, 4]:
+            if kwargs['region'] == 1 and storage_location in [3, 4]:
                 # 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['msg'] = 'success 1'
             else:
-                # AWS
-                img_url = DevicePushService.generate_s3_url(kwargs['aws_s3_client'], params)
-                res_data['img_push'] = img_url
+                # 华为云
+                img_url = DevicePushService.create_obs_signed_url(
+                    params['uid'], params['channel'], params['n_time'], 'PUT')
                 res_data['msg'] = 'success 1'
         elif is_st == 3:
             img_url_list = []
@@ -797,21 +801,20 @@ class DevicePushService:
             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'
+            if storage_location in [3, 4]:  # 三张图的时候提前获取实例化OCI
+                region = 'eur' if 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
                 if kwargs['region'] == 1:
                     # 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)
                 else:
-                    # AWS
-                    img_url = DevicePushService.generate_s3_url(kwargs['aws_s3_client'], params)
-                    img_url_list.append(img_url)
+                    # 华为云
+                    img_url = DevicePushService.create_obs_signed_url(
+                        params['uid'], params['channel'], params['n_time'], 'PUT')
             res_data['img_url_list'] = img_url_list
             res_data['msg'] = 'success 3'
         return res_data
@@ -828,6 +831,18 @@ class DevicePushService:
         )
         return response_url
 
+    @staticmethod
+    def create_obs_signed_url(uid, channel, n_time, method):
+        """
+        生成对象存储预签名URL
+        """
+        object_key = '{}/{}/{}.jpeg'.format(uid, channel, n_time)
+        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=object_key, expires=3600)
+        return res.signedUrl
+
     @staticmethod
     def check_share_permission(user_id, channel, uid):
         """