Showing
8 changed files
with
84 additions
and
19 deletions
1 | -""" | ||
2 | -Django settings for dcloud project. | ||
3 | - | ||
4 | -Generated by 'django-admin startproject' using Django 2.0.4. | ||
5 | - | ||
6 | -For more information on this file, see | ||
7 | -https://docs.djangoproject.com/en/2.0/topics/settings/ | ||
8 | - | ||
9 | -For the full list of settings and their values, see | ||
10 | -https://docs.djangoproject.com/en/2.0/ref/settings/ | ||
11 | -""" | ||
12 | - | ||
13 | import os | 1 | import os |
2 | +import aws_conf | ||
14 | 3 | ||
15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) | 4 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) |
16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | 5 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
... | @@ -39,7 +28,8 @@ INSTALLED_APPS = [ | ... | @@ -39,7 +28,8 @@ INSTALLED_APPS = [ |
39 | 'django.contrib.staticfiles', | 28 | 'django.contrib.staticfiles', |
40 | 'rest_framework', | 29 | 'rest_framework', |
41 | 'restful.apps.RestfulConfig', | 30 | 'restful.apps.RestfulConfig', |
42 | - 'website' | 31 | + 'website', |
32 | + 's3direct', | ||
43 | ] | 33 | ] |
44 | 34 | ||
45 | MIDDLEWARE = [ | 35 | MIDDLEWARE = [ |
... | @@ -125,4 +115,54 @@ STATIC_URL = '/static/' | ... | @@ -125,4 +115,54 @@ STATIC_URL = '/static/' |
125 | 115 | ||
126 | # Login redirect | 116 | # Login redirect |
127 | 117 | ||
128 | -LOGIN_REDIRECT_URL = '/' | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
118 | +LOGIN_REDIRECT_URL = '/' | ||
119 | + | ||
120 | +# AWS | ||
121 | + | ||
122 | +# If these are not defined, the EC2 instance profile and IAM role are used. | ||
123 | +# This requires you to add boto3 (or botocore, which is a dependency of boto3) | ||
124 | +# to your project dependencies. | ||
125 | +AWS_ACCESS_KEY_ID = aws_conf.AWS_ACCESS_KEY_ID | ||
126 | +AWS_SECRET_ACCESS_KEY = aws_conf.AWS_SECRET_ACCESS_KEY | ||
127 | + | ||
128 | +AWS_STORAGE_BUCKET_NAME = aws_conf.AWS_STORAGE_BUCKET_NAME | ||
129 | + | ||
130 | +# The region of your bucket, more info: | ||
131 | +# http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region | ||
132 | +S3DIRECT_REGION = 'ap-northeast-2' | ||
133 | + | ||
134 | +# Destinations, with the following keys: | ||
135 | +# | ||
136 | +# key [required] Where to upload the file to, can be either: | ||
137 | +# 1. '/' = Upload to root with the original filename. | ||
138 | +# 2. 'some/path' = Upload to some/path with the original filename. | ||
139 | +# 3. functionName = Pass a function and create your own path/filename. | ||
140 | +# key_args [optional] Arguments to be passed to 'key' if it's a function. | ||
141 | +# auth [optional] An ACL function to whether the current Django user can perform this action. | ||
142 | +# allowed [optional] List of allowed MIME types. | ||
143 | +# acl [optional] Give the object another ACL rather than 'public-read'. | ||
144 | +# cache_control [optional] Cache control headers, eg 'max-age=2592000'. | ||
145 | +# content_disposition [optional] Useful for sending files as attachments. | ||
146 | +# bucket [optional] Specify a different bucket for this particular object. | ||
147 | +# server_side_encryption [optional] Encryption headers for buckets that require it. | ||
148 | + | ||
149 | +S3DIRECT_DESTINATIONS = { | ||
150 | + 'example_destination': { | ||
151 | + # REQUIRED | ||
152 | + 'key': '/', | ||
153 | + | ||
154 | + # OPTIONAL | ||
155 | + #'auth': lambda u: u.is_staff, # Default allow anybody to upload | ||
156 | + #'allowed': ['image/jpeg', 'image/png', 'video/mp4'], # Default allow all mime types | ||
157 | + #'bucket': 'pdf-bucket', # Default is 'AWS_STORAGE_BUCKET_NAME' | ||
158 | + 'acl': 'private', # Defaults to 'public-read' | ||
159 | + 'cache_control': 'max-age=2592000', # Default no cache-control | ||
160 | + #'content_disposition': 'attachment', # Default no content disposition | ||
161 | + #'content_length_range': (5000, 20000000), # Default allow any size | ||
162 | + #'server_side_encryption': 'AES256', # Default no encryption | ||
163 | + }, | ||
164 | + 'example_other': { | ||
165 | + 'key': lambda filename, args: args + '/' + filename, | ||
166 | + 'key_args': 'uploads/images', # Only if 'key' is a function | ||
167 | + } | ||
168 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -10,5 +10,6 @@ urlpatterns = [ | ... | @@ -10,5 +10,6 @@ urlpatterns = [ |
10 | url(r'^accounts/login/$', views.login, name='login'), | 10 | url(r'^accounts/login/$', views.login, name='login'), |
11 | url(r'^accounts/logout/$', views.logout, name='logout', kwargs={'next_page': '/'}), | 11 | url(r'^accounts/logout/$', views.logout, name='logout', kwargs={'next_page': '/'}), |
12 | 12 | ||
13 | + url(r'^s3direct/', include('s3direct.urls')), | ||
13 | ] | 14 | ] |
14 | 15 | ... | ... |
1 | from django import forms | 1 | from django import forms |
2 | +from s3direct.widgets import S3DirectWidget | ||
2 | 3 | ||
3 | -# class PostForm(forms.ModelForm): | ||
4 | -# class Meta: | ||
5 | -# model = Post | ||
6 | -# fields = ('title', 'text') | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
4 | +class S3DirectUploadForm(forms.Form): | ||
5 | + images = forms.URLField(widget=S3DirectWidget(dest='example_destination')) | ... | ... |
1 | from django.db import models | 1 | from django.db import models |
2 | -from django.utils import timezone | 2 | +from s3direct.fields import S3DirectField |
3 | 3 | ||
4 | +class Example(models.Model): | ||
5 | + video = S3DirectField(dest='example_destination') | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -11,4 +11,5 @@ urlpatterns = [ | ... | @@ -11,4 +11,5 @@ urlpatterns = [ |
11 | # blog | 11 | # blog |
12 | url(r'^$', views.home), | 12 | url(r'^$', views.home), |
13 | url(r'^files/', views.file_list, name='file_list'), | 13 | url(r'^files/', views.file_list, name='file_list'), |
14 | + url(r'^upload/', views.upload.as_view(), name='upload') | ||
14 | ] | 15 | ] |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
1 | from django.shortcuts import render, get_object_or_404, redirect, Http404 | 1 | from django.shortcuts import render, get_object_or_404, redirect, Http404 |
2 | from django.utils import timezone | 2 | from django.utils import timezone |
3 | from django.contrib.auth.decorators import login_required | 3 | from django.contrib.auth.decorators import login_required |
4 | +from django.views.generic import FormView | ||
5 | +from website.forms import S3DirectUploadForm | ||
4 | from restful.models import File | 6 | from restful.models import File |
5 | import requests | 7 | import requests |
6 | 8 | ||
... | @@ -14,3 +16,8 @@ def file_list(request): | ... | @@ -14,3 +16,8 @@ def file_list(request): |
14 | files = requests.get('http://localhost:8000/restapi/files') | 16 | files = requests.get('http://localhost:8000/restapi/files') |
15 | files = files.json() | 17 | files = files.json() |
16 | return render(request, 'website/file_list.html', files) | 18 | return render(request, 'website/file_list.html', files) |
19 | + | ||
20 | + | ||
21 | +class upload(FormView): | ||
22 | + template_name = 'website/s3direct.html' | ||
23 | + form_class = S3DirectUploadForm | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment