Ver código fonte

服务器没有图片文件查询aws s3

locky 3 anos atrás
pai
commit
60886dd890
1 arquivos alterados com 13 adições e 8 exclusões
  1. 13 8
      Controller/UserManger.py

+ 13 - 8
Controller/UserManger.py

@@ -189,9 +189,10 @@ class getAvatarView(TemplateView):
 
     def getAvatar(self, filePath):
         response = ResponseObject()
-        if filePath == '' or filePath == None:
+        if not filePath:
             return response.json(800)
         if filePath == 'User/default.png' or filePath == 'User/defaultUser.png':
+            # 使用默认头像
             try:
                 aws_s3_client = boto3.client(
                     's3',
@@ -207,21 +208,25 @@ class getAvatarView(TemplateView):
                 return response.json(500, repr(e))
         
         fullPath = os.path.join(BASE_DIR, "static", filePath).replace('\\', '/')
-        defaultPath = os.path.join(BASE_DIR, "static", "User/default.png").replace('\\', '/')
         if os.path.isfile(fullPath):
             try:
-                Imagedata = open(fullPath, 'rb').read()
+                imageData = open(fullPath, 'rb').read()
+                return HttpResponse(imageData, content_type="image/jpeg")
             except Exception as e:
                 return response.json(906, repr(e))
-            else:
-                return HttpResponse(Imagedata, content_type="image/jpeg")
         else:
             try:
-                Imagedata = open(defaultPath, 'rb').read()
+                aws_s3_client = boto3.client(
+                    's3',
+                    region_name=REGION_NAME,
+                    aws_access_key_id=ACCESS_KEY_ID,
+                    aws_secret_access_key=SECRET_ACCESS_KEY,
+                    config=botocore.client.Config(signature_version='s3v4'),
+                )
+                get_object_response = aws_s3_client.get_object(Bucket=AVATAR_BUCKET, Key=filePath)
+                return HttpResponse(get_object_response['Body'], content_type="image/jpeg")
             except Exception as e:
                 return response.json(906, repr(e))
-            else:
-                return HttpResponse(Imagedata, content_type="image/jpeg")
 
 
 @csrf_exempt