신은섭(Shin Eun Seop)

add s3direct

......@@ -105,3 +105,6 @@ ENV/
# mypy
.mypy_cache/
# aws configutation file
aws_conf.py
......
"""
Django settings for dcloud project.
Generated by 'django-admin startproject' using Django 2.0.4.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""
import os
import aws_conf
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
......@@ -39,7 +28,8 @@ INSTALLED_APPS = [
'django.contrib.staticfiles',
'rest_framework',
'restful.apps.RestfulConfig',
'website'
'website',
's3direct',
]
MIDDLEWARE = [
......@@ -125,4 +115,54 @@ STATIC_URL = '/static/'
# Login redirect
LOGIN_REDIRECT_URL = '/'
\ No newline at end of file
LOGIN_REDIRECT_URL = '/'
# AWS
# If these are not defined, the EC2 instance profile and IAM role are used.
# This requires you to add boto3 (or botocore, which is a dependency of boto3)
# to your project dependencies.
AWS_ACCESS_KEY_ID = aws_conf.AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY = aws_conf.AWS_SECRET_ACCESS_KEY
AWS_STORAGE_BUCKET_NAME = aws_conf.AWS_STORAGE_BUCKET_NAME
# The region of your bucket, more info:
# http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
S3DIRECT_REGION = 'ap-northeast-2'
# Destinations, with the following keys:
#
# key [required] Where to upload the file to, can be either:
# 1. '/' = Upload to root with the original filename.
# 2. 'some/path' = Upload to some/path with the original filename.
# 3. functionName = Pass a function and create your own path/filename.
# key_args [optional] Arguments to be passed to 'key' if it's a function.
# auth [optional] An ACL function to whether the current Django user can perform this action.
# allowed [optional] List of allowed MIME types.
# acl [optional] Give the object another ACL rather than 'public-read'.
# cache_control [optional] Cache control headers, eg 'max-age=2592000'.
# content_disposition [optional] Useful for sending files as attachments.
# bucket [optional] Specify a different bucket for this particular object.
# server_side_encryption [optional] Encryption headers for buckets that require it.
S3DIRECT_DESTINATIONS = {
'example_destination': {
# REQUIRED
'key': '/',
# OPTIONAL
#'auth': lambda u: u.is_staff, # Default allow anybody to upload
#'allowed': ['image/jpeg', 'image/png', 'video/mp4'], # Default allow all mime types
#'bucket': 'pdf-bucket', # Default is 'AWS_STORAGE_BUCKET_NAME'
'acl': 'private', # Defaults to 'public-read'
'cache_control': 'max-age=2592000', # Default no cache-control
#'content_disposition': 'attachment', # Default no content disposition
#'content_length_range': (5000, 20000000), # Default allow any size
#'server_side_encryption': 'AES256', # Default no encryption
},
'example_other': {
'key': lambda filename, args: args + '/' + filename,
'key_args': 'uploads/images', # Only if 'key' is a function
}
}
\ No newline at end of file
......
......@@ -10,5 +10,6 @@ urlpatterns = [
url(r'^accounts/login/$', views.login, name='login'),
url(r'^accounts/logout/$', views.logout, name='logout', kwargs={'next_page': '/'}),
url(r'^s3direct/', include('s3direct.urls')),
]
......
from django import forms
from s3direct.widgets import S3DirectWidget
# class PostForm(forms.ModelForm):
# class Meta:
# model = Post
# fields = ('title', 'text')
\ No newline at end of file
class S3DirectUploadForm(forms.Form):
images = forms.URLField(widget=S3DirectWidget(dest='example_destination'))
......
from django.db import models
from django.utils import timezone
from s3direct.fields import S3DirectField
class Example(models.Model):
video = S3DirectField(dest='example_destination')
\ No newline at end of file
......
<html>
<head>
<meta charset="utf-8">
<title>s3direct</title>
{{ form.media }}
</head>
<body>
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
</form>
</body>
</html>
\ No newline at end of file
......@@ -11,4 +11,5 @@ urlpatterns = [
# blog
url(r'^$', views.home),
url(r'^files/', views.file_list, name='file_list'),
url(r'^upload/', views.upload.as_view(), name='upload')
]
\ No newline at end of file
......
from django.shortcuts import render, get_object_or_404, redirect, Http404
from django.utils import timezone
from django.contrib.auth.decorators import login_required
from django.views.generic import FormView
from website.forms import S3DirectUploadForm
from restful.models import File
import requests
......@@ -14,3 +16,8 @@ def file_list(request):
files = requests.get('http://localhost:8000/restapi/files')
files = files.json()
return render(request, 'website/file_list.html', files)
class upload(FormView):
template_name = 'website/s3direct.html'
form_class = S3DirectUploadForm
\ No newline at end of file
......