최지우

add Video Download API

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