Browse Source

优化反馈推送图片不准确接口

locky 2 năm trước cách đây
mục cha
commit
03a06cf746
2 tập tin đã thay đổi với 44 bổ sung12 xóa
  1. 31 11
      Controller/FeedBack.py
  2. 13 1
      Object/AWS/AmazonS3Util.py

+ 31 - 11
Controller/FeedBack.py

@@ -304,13 +304,41 @@ class FeedBackView(View):
         if not all([equipment_info_id, uid, is_st, event_type, event_time, channel]):
             return response.json(444)
         is_st, event_type, channel = int(is_st), int(event_type), int(channel)
+        # 查询数据是否存在
         push_inaccurate_feedback_qs = PushInaccurateFeedback.objects.filter(equipment_info_id=equipment_info_id,
                                                                             user_id=user_id, uid=uid, channel=channel,
                                                                             event_type=event_type, event_time=event_time,
                                                                             is_st=is_st)
         if push_inaccurate_feedback_qs.exists():
             return response.json(174)
-        return response.json(0)
+        # 查询图片是否存在s3
+        if CONFIG_INFO == CONFIG_TEST or CONFIG_INFO == CONFIG_CN:  # 国内
+            region_name = AWS_IOT_SES_ACCESS_CHINA_REGION
+            aws_access_key_id = AWS_ACCESS_KEY_ID[0]
+            secret_access_key = AWS_SECRET_ACCESS_KEY[0]
+            bucket = 'push'
+        else:  # 国外
+            region_name = AWS_SES_ACCESS_REGION
+            aws_access_key_id = AWS_ACCESS_KEY_ID[1]
+            secret_access_key = AWS_SECRET_ACCESS_KEY[1]
+            bucket = 'foreignpush'
+        if is_st == 3:
+            key = '{uid}/{channel}/{event_time}.jpeg'.format(uid=uid, channel=channel, event_time=event_time)
+        else:
+            key = '{uid}/{channel}/{event_time}_0.jpeg'.format(uid=uid, channel=channel, event_time=event_time)
+        try:
+            s3 = AmazonS3Util(
+                aws_access_key_id=aws_access_key_id,
+                secret_access_key=secret_access_key,
+                region_name=region_name
+            )
+            have_object = s3.get_object(bucket, key)
+            if have_object:
+                return response.json(0)
+            else:
+                return response.json(174)
+        except Exception as e:
+            return response.json(500, repr(e))
 
     @staticmethod
     def push_inaccurate(user_id, request_dict, response):
@@ -345,22 +373,14 @@ class FeedBackView(View):
                 file_path = '{uid}/{channel}/{event_time}.jpeg'.format(uid=uid,
                                                                        channel=channel,
                                                                        event_time=event_time)
-                try:
-                    s3.copy_obj(source_bucket, 'push-inaccurate', file_path)
-                except Exception as e:
-                    if e.response['Error']['Code'] == "404":
-                        return response.json(173)
+                s3.copy_obj(source_bucket, 'push-inaccurate', file_path)
             else:
                 for index in range(3):
                     file_path = '{uid}/{channel}/{event_time}_{index}.jpeg'.format(uid=uid,
                                                                                    channel=channel,
                                                                                    event_time=event_time,
                                                                                    index=index)
-                    try:
-                        s3.copy_obj(source_bucket, 'push-inaccurate', file_path)
-                    except Exception as e:
-                        if e.response['Error']['Code'] == "404":
-                            continue
+                    s3.copy_obj(source_bucket, 'push-inaccurate', file_path)
             PushInaccurateFeedback.objects.create(equipment_info_id=equipment_info_id,
                                                   user_id=user_id, event_type=event_type,
                                                   uid=uid, channel=channel, add_time=now_time,

+ 13 - 1
Object/AWS/AmazonS3Util.py

@@ -111,6 +111,19 @@ class AmazonS3Util:
             exists = False
         return exists
 
+    def get_object(self, bucket, key):
+        """
+        获取对象
+        @param bucket: 存储桶
+        @param key: 文件
+        @return : boolean
+        """
+        try:
+            self.client_conn.get_object(Bucket=bucket, Key=key)
+            return True
+        except self.client_conn.exceptions.NoSuchKey:
+            return False
+
     def copy_obj(self, source_bucket, to_bucket, file_key):
         """
         复制对象
@@ -123,4 +136,3 @@ class AmazonS3Util:
             'Key': file_key
         }
         self.session_conn.meta.client.copy(source_dict, to_bucket, file_key)
-