김재형

Fix s3

...@@ -22,7 +22,7 @@ from django.conf import settings ...@@ -22,7 +22,7 @@ from django.conf import settings
22 import jwt 22 import jwt
23 from django.http import HttpResponse, JsonResponse 23 from django.http import HttpResponse, JsonResponse
24 from khudrive.settings import AWS_SESSION_TOKEN, AWS_SECRET_ACCESS_KEY, AWS_ACCESS_KEY_ID, AWS_REGION, \ 24 from khudrive.settings import AWS_SESSION_TOKEN, AWS_SECRET_ACCESS_KEY, AWS_ACCESS_KEY_ID, AWS_REGION, \
25 - AWS_STORAGE_BUCKET_NAME 25 + AWS_STORAGE_BUCKET_NAME, AWS_ENDPOINT_URL
26 26
27 27
28 class UserViewSet(viewsets.ModelViewSet): 28 class UserViewSet(viewsets.ModelViewSet):
...@@ -183,11 +183,15 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -183,11 +183,15 @@ class ItemViewSet(viewsets.ViewSet):
183 # url: items/11/ 183 # url: items/11/
184 # 마지막 slash도 써주어야함 184 # 마지막 slash도 써주어야함
185 def get(self, request, pk): 185 def get(self, request, pk):
186 - s3 = boto3.client('s3', 186 + s3 = boto3.client(
187 - aws_access_key_id=AWS_ACCESS_KEY_ID, 187 + 's3',
188 - aws_secret_access_key=AWS_SECRET_ACCESS_KEY, 188 + region_name=AWS_REGION,
189 - aws_session_token=AWS_SESSION_TOKEN, 189 + aws_access_key_id=AWS_ACCESS_KEY_ID,
190 - config=Config(signature_version='s3v4')) 190 + aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
191 + aws_session_token=AWS_SESSION_TOKEN,
192 + endpoint_url=AWS_ENDPOINT_URL or None,
193 + config=Config(s3={'addressing_style': 'path'})
194 + )
191 s3_bucket = AWS_STORAGE_BUCKET_NAME 195 s3_bucket = AWS_STORAGE_BUCKET_NAME
192 196
193 item = Item.objects.filter(item_id=pk) 197 item = Item.objects.filter(item_id=pk)
...@@ -329,7 +333,7 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -329,7 +333,7 @@ class ItemViewSet(viewsets.ViewSet):
329 url_path='children', url_name='children') 333 url_path='children', url_name='children')
330 def children(self, request, pk): 334 def children(self, request, pk):
331 if request.method == 'GET': 335 if request.method == 'GET':
332 - children = Item.objects.filter(parent=pk, is_deleted=False) 336 + children = Item.objects.filter(parent=pk, is_deleted=False, status=True)
333 children_data = serializers.serialize("json", children) 337 children_data = serializers.serialize("json", children)
334 json_children = json.loads(children_data) 338 json_children = json.loads(children_data)
335 parent = Item.objects.filter(item_id=pk) # item 339 parent = Item.objects.filter(item_id=pk) # item
...@@ -380,7 +384,15 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -380,7 +384,15 @@ class ItemViewSet(viewsets.ViewSet):
380 url_path='upload', url_name='upload') 384 url_path='upload', url_name='upload')
381 def upload(self, request, pk): 385 def upload(self, request, pk):
382 if request.method == 'POST': 386 if request.method == 'POST':
383 - s3 = boto3.client('s3') 387 + s3 = boto3.client(
388 + 's3',
389 + region_name=AWS_REGION,
390 + aws_access_key_id=AWS_ACCESS_KEY_ID,
391 + aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
392 + aws_session_token=AWS_SESSION_TOKEN,
393 + endpoint_url=AWS_ENDPOINT_URL or None,
394 + config=Config(s3={'addressing_style': 'path'})
395 + )
384 s3_bucket = AWS_STORAGE_BUCKET_NAME 396 s3_bucket = AWS_STORAGE_BUCKET_NAME
385 397
386 # 파일 객체 생성 398 # 파일 객체 생성
...@@ -412,12 +424,19 @@ class ItemViewSet(viewsets.ViewSet): ...@@ -412,12 +424,19 @@ class ItemViewSet(viewsets.ViewSet):
412 3600 424 3600
413 ) 425 )
414 426
427 + item = Item.objects.filter(item_id=upload_item.item_id)
428 + item_data = serializers.serialize("json", item)
429 + json_item = json.loads(item_data)
430 + res = json_item[0]['fields']
431 + res['id'] = json_item[0]['pk']
432 +
415 data = { 433 data = {
416 "signed_url": presigned_post, 434 "signed_url": presigned_post,
417 - 'url': 'https://%s.s3.amazonaws.com/%s' % (s3_bucket, file_name) 435 + 'url': '%s/%s' % (presigned_post["url"], file_name),
436 + 'item': res
418 } 437 }
419 438
420 - return Response({'presigned_post': presigned_post, 'proc_data': data}, status=status.HTTP_200_OK) 439 + return Response(data, status=status.HTTP_200_OK)
421 440
422 # url: /status/ 441 # url: /status/
423 @action(methods=['POST'], detail=True, permission_classes=[AllowAny], 442 @action(methods=['POST'], detail=True, permission_classes=[AllowAny],
......
1 +{
2 + "AWS_SESSION_TOKEN": "",
3 + "AWS_SECRET_ACCESS_KEY": "secret_key",
4 + "AWS_ACCESS_KEY_ID": "access_key",
5 + "AWS_REGION": "us-west-2",
6 + "AWS_STORAGE_BUCKET_NAME": "bucket",
7 + "AWS_ENDPOINT_URL": "http://localhost:39000"
8 +}
...\ No newline at end of file ...\ No newline at end of file