김재형

Fix s3

......@@ -22,7 +22,7 @@ from django.conf import settings
import jwt
from django.http import HttpResponse, JsonResponse
from khudrive.settings import AWS_SESSION_TOKEN, AWS_SECRET_ACCESS_KEY, AWS_ACCESS_KEY_ID, AWS_REGION, \
AWS_STORAGE_BUCKET_NAME
AWS_STORAGE_BUCKET_NAME, AWS_ENDPOINT_URL
class UserViewSet(viewsets.ModelViewSet):
......@@ -183,11 +183,15 @@ class ItemViewSet(viewsets.ViewSet):
# url: items/11/
# 마지막 slash도 써주어야함
def get(self, request, pk):
s3 = boto3.client('s3',
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
aws_session_token=AWS_SESSION_TOKEN,
config=Config(signature_version='s3v4'))
s3 = boto3.client(
's3',
region_name=AWS_REGION,
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
aws_session_token=AWS_SESSION_TOKEN,
endpoint_url=AWS_ENDPOINT_URL or None,
config=Config(s3={'addressing_style': 'path'})
)
s3_bucket = AWS_STORAGE_BUCKET_NAME
item = Item.objects.filter(item_id=pk)
......@@ -329,7 +333,7 @@ class ItemViewSet(viewsets.ViewSet):
url_path='children', url_name='children')
def children(self, request, pk):
if request.method == 'GET':
children = Item.objects.filter(parent=pk, is_deleted=False)
children = Item.objects.filter(parent=pk, is_deleted=False, status=True)
children_data = serializers.serialize("json", children)
json_children = json.loads(children_data)
parent = Item.objects.filter(item_id=pk) # item
......@@ -380,7 +384,15 @@ class ItemViewSet(viewsets.ViewSet):
url_path='upload', url_name='upload')
def upload(self, request, pk):
if request.method == 'POST':
s3 = boto3.client('s3')
s3 = boto3.client(
's3',
region_name=AWS_REGION,
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
aws_session_token=AWS_SESSION_TOKEN,
endpoint_url=AWS_ENDPOINT_URL or None,
config=Config(s3={'addressing_style': 'path'})
)
s3_bucket = AWS_STORAGE_BUCKET_NAME
# 파일 객체 생성
......@@ -412,12 +424,19 @@ class ItemViewSet(viewsets.ViewSet):
3600
)
item = Item.objects.filter(item_id=upload_item.item_id)
item_data = serializers.serialize("json", item)
json_item = json.loads(item_data)
res = json_item[0]['fields']
res['id'] = json_item[0]['pk']
data = {
"signed_url": presigned_post,
'url': 'https://%s.s3.amazonaws.com/%s' % (s3_bucket, file_name)
'url': '%s/%s' % (presigned_post["url"], file_name),
'item': res
}
return Response({'presigned_post': presigned_post, 'proc_data': data}, status=status.HTTP_200_OK)
return Response(data, status=status.HTTP_200_OK)
# url: /status/
@action(methods=['POST'], detail=True, permission_classes=[AllowAny],
......
{
"AWS_SESSION_TOKEN": "",
"AWS_SECRET_ACCESS_KEY": "secret_key",
"AWS_ACCESS_KEY_ID": "access_key",
"AWS_REGION": "us-west-2",
"AWS_STORAGE_BUCKET_NAME": "bucket",
"AWS_ENDPOINT_URL": "http://localhost:39000"
}
\ No newline at end of file