Toggle navigation
Toggle navigation
This project
Loading...
Sign in
2020-1-CloudComputing
/
C_Team_KhuDrive
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Authored by
권주희
2020-06-14 23:08:13 +0900
Browse Files
Options
Browse Files
Download
Plain Diff
Commit
86b3408a21ee5f7e3e5a8a1bef1e81ed632b9c0b
86b3408a
2 parents
ef348fa9
34c1a681
Merge branch 'feature/item_api+folder_api' into develop
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
117 additions
and
6 deletions
.gitignore
backend/api/models.py
backend/api/views.py
backend/khudrive/settings.py
backend/khudrive/urls.py
.gitignore
View file @
86b3408
...
...
@@ -27,3 +27,4 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
.idea
secrets.json
...
...
backend/api/models.py
View file @
86b3408
...
...
@@ -14,7 +14,7 @@ class Item(models.Model):
is_deleted
=
models
.
BooleanField
(
default
=
False
)
created_time
=
models
.
DateTimeField
(
auto_now
=
True
)
updated_time
=
models
.
DateTimeField
(
null
=
True
)
status
=
models
.
BooleanField
()
status
=
models
.
BooleanField
(
default
=
False
)
#file = models.FileField(upload_to = \path)
...
...
backend/api/views.py
View file @
86b3408
...
...
@@ -4,6 +4,7 @@ import os
from
datetime
import
datetime
,
timedelta
import
boto3
from
botocore.client
import
Config
from
django.core
import
serializers
from
django.views.decorators.csrf
import
csrf_exempt
...
...
@@ -20,7 +21,7 @@ from annoying.functions import get_object_or_None
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
class
UserViewSet
(
viewsets
.
ModelViewSet
):
"""
...
...
@@ -144,15 +145,51 @@ class ItemViewSet(viewsets.ViewSet):
t
[
'id'
]
=
i
[
'pk'
]
res
.
append
(
t
)
return
Response
({
'data'
:
{
'list'
:
res
}},
status
=
status
.
HTTP_200_OK
)
"""
# url: items/11/
# 마지막 slash도 써주어야함
def get(self, request, pk):
#print(pk)
s3 = boto3.client('s3')
s3_bucket = AWS_STORAGE_BUCKET_NAME
#파일 객체 생성
object_name = request.GET.get('name', '')
presigned_url = s3.generate_presigned_url(
'get_object',
Params={'Bucket': s3_bucket,
'Key': object_name},
ExpiresIn = 3600
)
return Response({'message': presigned_url}, status=status.HTTP_200_OK)
"""
# 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_bucket
=
AWS_STORAGE_BUCKET_NAME
item
=
Item
.
objects
.
filter
(
item_id
=
pk
)
object_name
=
item
.
get
()
.
name
data
=
serializers
.
serialize
(
"json"
,
item
)
json_data
=
json
.
loads
(
data
)
presigned_url
=
s3
.
generate_presigned_url
(
'get_object'
,
Params
=
{
'Bucket'
:
s3_bucket
,
'Key'
:
object_name
},
ExpiresIn
=
3600
)
res
=
json_data
[
0
][
'fields'
]
res
[
'id'
]
=
json_data
[
0
][
'pk'
]
res
[
'signed_url'
]
=
presigned_url
return
Response
({
'data'
:
res
},
status
=
status
.
HTTP_200_OK
)
# url: items/11/
...
...
@@ -273,6 +310,63 @@ class ItemViewSet(viewsets.ViewSet):
res
[
'inside_file_list'
]
=
[]
return
Response
({
'data'
:
res
},
status
=
status
.
HTTP_200_OK
)
# url: /upload/
@action
(
methods
=
[
'POST'
],
detail
=
True
,
permission_classes
=
[
AllowAny
],
url_path
=
'upload'
,
url_name
=
'upload'
)
def
upload
(
self
,
request
,
pk
):
if
request
.
method
==
'POST'
:
s3
=
boto3
.
client
(
's3'
)
s3_bucket
=
AWS_STORAGE_BUCKET_NAME
#파일 객체 생성
file_name
=
request
.
POST
.
get
(
'name'
,
''
)
file_size
=
request
.
POST
.
get
(
'size'
,
''
)
file_parent
=
pk
file_type
=
mimetypes
.
guess_type
(
file_name
)[
0
]
upload_item
=
Item
(
name
=
file_name
,
size
=
file_size
,
user_id
=
1
,
file_type
=
file_type
,
parent
=
file_parent
)
upload_item
.
save
()
date_long
=
datetime
.
utcnow
()
.
strftime
(
'
%
Y
%
m
%
dT000000Z'
)
presigned_post
=
s3
.
generate_presigned_post
(
s3_bucket
,
file_name
,
{
"acl"
:
"private"
,
"Content-Type"
:
file_type
,
'region'
:
AWS_REGION
,
'x-amz-algorithm'
:
'AWS4-HMAC-SHA256'
,
'x-amz-date'
:
date_long
},
[
{
"acl"
:
"private"
},
{
"Content-Type"
:
file_type
},
{
'x-amz-algorithm'
:
'AWS4-HMAC-SHA256'
},
{
'x-amz-date'
:
date_long
}
],
3600
)
data
=
{
"signed_url"
:
presigned_post
,
'url'
:
'https://
%
s.s3.amazonaws.com/
%
s'
%
(
s3_bucket
,
file_name
)
}
return
Response
({
'presigned_post'
:
presigned_post
,
'proc_data'
:
data
},
status
=
status
.
HTTP_200_OK
)
# url: /status/
@action
(
methods
=
[
'POST'
],
detail
=
True
,
permission_classes
=
[
AllowAny
],
url_path
=
'status'
,
url_name
=
'status'
)
def
status
(
self
,
request
,
*
args
,
**
kwargs
):
if
request
.
method
==
'POST'
:
pk
=
request
.
POST
.
get
(
'item_id'
,
''
)
queryset
=
Item
.
objects
.
filter
(
item_id
=
pk
)
for
cand
in
queryset
:
cand
.
status
=
True
cand
.
save
()
return
Response
({
'Message'
:
'File Upload Successful'
},
status
=
status
.
HTTP_200_OK
)
return
Response
({
'Error'
:
'No such item found in queryset'
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
class
SharedItemViewSet
(
viewsets
.
ModelViewSet
):
...
...
backend/khudrive/settings.py
View file @
86b3408
...
...
@@ -11,10 +11,17 @@ https://docs.djangoproject.com/en/3.0/ref/settings/
"""
import
os
import
sys
import
json
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR
=
os
.
path
.
dirname
(
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
)))
ROOT_DIR
=
os
.
path
.
dirname
(
BASE_DIR
)
# secrets.json의 경로
SECRETS_PATH
=
os
.
path
.
join
(
ROOT_DIR
,
'secrets.json'
)
# json파일을 파이썬 객체로 변환
secrets
=
json
.
loads
(
open
(
SECRETS_PATH
)
.
read
())
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
...
...
@@ -82,8 +89,8 @@ DATABASES = {
'default'
:
{
'ENGINE'
:
'django.db.backends.postgresql'
,
'NAME'
:
'khuDrive'
,
'USER'
:
'
jooheekwon
'
,
'PASSWORD'
:
''
,
'USER'
:
'
root
'
,
'PASSWORD'
:
'
1234
'
,
'HOST'
:
'localhost'
,
'PORT'
:
''
,
}
...
...
@@ -127,3 +134,11 @@ USE_TZ = True
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL
=
'/static/'
#S3
DEFAULT_FILE_STORAGE
=
'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE
=
'storages.backends.s3boto3.S3Boto3Storage'
for
key
,
value
in
secrets
.
items
():
setattr
(
sys
.
modules
[
__name__
],
key
,
value
)
\ No newline at end of file
...
...
backend/khudrive/urls.py
View file @
86b3408
...
...
@@ -33,8 +33,9 @@ urlpatterns = [
url
(
r'^<int:pk>/share/$'
,
views
.
SharedItemViewSet
.
share
,
name
=
'share'
),
url
(
r'^<int:pk>/move/$'
,
views
.
ItemViewSet
.
move
,
name
=
'move'
),
url
(
r'^<int:pk>/copy/$'
,
views
.
ItemViewSet
.
copy
,
name
=
'copy'
),
url
(
r'^<int:pk>/children/$'
,
views
.
ItemViewSet
.
children
,
name
=
'c
opy
'
),
url
(
r'^<int:pk>/children/$'
,
views
.
ItemViewSet
.
children
,
name
=
'c
hildren
'
),
url
(
r'^signup/$'
,
views
.
UserViewSet
.
signup
,
name
=
'signup'
),
url
(
r'^login/$'
,
views
.
UserViewSet
.
login
,
name
=
'login'
),
url
(
r'^upload/$'
,
views
.
ItemViewSet
.
upload
,
name
=
'upload'
),
url
(
r'^status/$'
,
views
.
ItemViewSet
.
status
,
name
=
'status'
),
]
...
...
Please
register
or
login
to post a comment