최지우

add Video Download API

{
"aws" : {
"access_key_id" : "AKIA5ZZ6DPFONI4VDIPH",
"secret_access_key" : "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",
"s3_bucket_name" : "khuproject2020",
"s3_access_url" : "https://khuproject2020.s3.ap-northeast-2.amazonaws.com/"
}
}
\ No newline at end of file
......@@ -4,3 +4,4 @@ __pycache__
db.sqlite3
/static
.DS_Store
.aws_key
......
from api.models import Phone, Video
from rest_framework import serializers
class PhoneSerializer(serializers.ModelSerializer) :
class Meta :
model = models.Phone
model = Phone
fields = '__all__'
class VideoSerializer(serializers.ModelSerializer) :
class Meta :
model = models.Video
model = Video
fields = '__all__'
......
import boto3
from django.http import HttpResponse
from django.core import serializers
from django.core.exceptions import FieldDoesNotExist, ObjectDoesNotExist
from django.shortcuts import render
from api.models import Video, Phone
from api.serializers import VideoSerializer, PhoneSerializer
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.request import Request
from rest_framework.response import Response
from boto3.session import Session
from src.settings import AWS_REGION
from src.settings import S3_ACCESS_URL
from datetime import datetime, timedelta
# Create your views here.
class VideoDownload(APIView) :
def get(self, request, format=None) :
try :
request_id = request.GET.get('vidname')
if request_id == 'None' :
raise FieldDoesNotExist
download_url = S3_ACCESS_URL + str(request_id)
if not download_url :
raise ObjectDoesNotExist
res = {
's3_link' : download_url
}
return Response(res, status = status.HTTP_200_OK)
except FieldDoesNotExist as error :
return Response({
'error' : "FieldDoesNotExist ",
'date' : datetime.now()
}, status = status.HTTP_400_BAD_REQUEST)
except ObjectDoesNotExist as error :
return Response({
'error' : "ObjectDoesNotExist",
'date' : datetime.now()
}, status = status.HTTP_404_NOT_FOUND)
......
Django==3.1.2
django==3.1.2
djangorestframework==3.12.1
sqlparse==0.3.1
pytz==2020.1
django-cors-headers==3.5.0
boto3==1.15.15
......
......@@ -9,7 +9,7 @@ https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
import os
import json
from pathlib import Path
......@@ -123,3 +123,12 @@ USE_TZ = True
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'
AWS_REGION = 'ap-northeast-2'
AWS_SETTINGS = os.path.join(BASE_DIR, '.aws_key.json')
awskey = json.loads(open(AWS_SETTINGS).read())
S3_ACCESS_KEY_ID = awskey['aws']['access_key_id']
S3_SECRET_ACCESS_KEY = awskey['aws']['secret_access_key']
S3_STORAGE_BUCKET_NAME = awskey['aws']['s3_bucket_name']
S3_ACCESS_URL = awskey['aws']['s3_access_url']
......
......@@ -14,8 +14,11 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from rest_framework import routers
from django.urls import path, include
from api import views
urlpatterns = [
path('admin/', admin.site.urls),
path('api/videoDownload/', views.VideoDownload.as_view()),
]
......