Skip to content

Commit

Permalink
minio storage
Browse files Browse the repository at this point in the history
  • Loading branch information
qtrinh2 committed Jul 2, 2024
1 parent 0c884a7 commit 5a6b464
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 6 deletions.
40 changes: 39 additions & 1 deletion django/django_test/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from dotenv import load_dotenv

import environ
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand Down Expand Up @@ -45,6 +46,7 @@
'django.contrib.staticfiles',
'tailwind',
'theme',
'upload',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -124,11 +126,47 @@
USE_TZ = True


STORAGES = {
"default": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"staticfiles": {
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
},
}

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/

STATIC_URL = 'static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
STATIC_ROOT = str(BASE_DIR / "staticfiles")
STATIC_URL = 'static/'

# Media files

USE_S3 = env("USE_S3", default=False)
if USE_S3:
AWS_ACCESS_KEY_ID = 'iTxhV669tiWq0j0HF5gT'
AWS_SECRET_ACCESS_KEY = 'nnNJu7OGGjyYjk55hWoPdIGBPznicbDRJ9A8RmeQ'
AWS_STORAGE_BUCKET_NAME = 'test'
AWS_S3_ENDPOINT_URL='http://10.112.113.212:8000'

#STATIC_URL = 'static/'
#STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)

MEDIA_URL = f'{AWS_S3_ENDPOINT_URL}/{AWS_STORAGE_BUCKET_NAME}/'
#MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles')

# override default storage backend for media
STORAGES["default"] = {
"BACKEND": "storages.backends.s3.S3Storage",
}

else:
MEDIA_URL = "media/"
MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles')



# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
Expand Down
4 changes: 3 additions & 1 deletion django/django_test/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
from django.urls import path

from . import views
from upload.views import image_upload

urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('upload/', image_upload, name='upload'),
]
5 changes: 3 additions & 2 deletions django/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ services:
- DB_NAME=db_dj
- DB_USER=user_dj
- DB_PASS=password
- USE_S3=True
ports:
- "1337:8000"
command: >
sh -c "poetry run python manage.py migrate &&
poetry run python manage.py runserver 0.0.0.0:8000"
#volumes:
#- .:/app
#volumes:
#- .:/app
depends_on:
db:
condition: service_healthy
Expand Down
22 changes: 22 additions & 0 deletions django/upload/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.2.13 on 2024-07-02 16:02

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Upload',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('uploaded_at', models.DateTimeField(auto_now_add=True)),
('file', models.FileField(upload_to='')),
],
),
]
4 changes: 3 additions & 1 deletion django/upload/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.db import models

# Create your models here.
class Upload(models.Model):
uploaded_at = models.DateTimeField(auto_now_add=True)
file = models.FileField()
57 changes: 57 additions & 0 deletions django/upload/templates/upload.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{% load static tailwind_tags %}

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="shortcut icon" type="image/png" href="{% static 'images/favicon.png' %}">
<title>Image Upload</title>
{% tailwind_css %}
</head>
<body>
<div class="container">
<div class="section">
<h1 class="is-size-1">Upload Image</h1>
<hr><br>
<form action="{% url "upload" %}" method="post" enctype="multipart/form-data" class="form">
{% csrf_token %}
<div class="field">
<div class="control">
<div class="file">
<label class="file-label">
<input class="file-input" type="file" name="image_file">
<span class="file-cta">
<span class="file-label">Choose a file...</span>
</span>
</label>
</div>
</div>
</div>
<div class="field">
<div class="control">
<label class="radio">
<input type="radio" name="image_type" value='public' checked>
Public
</label>
<label class="radio">
<input type="radio" name="image_type" value='private'>
Private
</label>
</div>
</div>
<br>
<div class="field">
<div class="control">
<input type="submit" value="Submit" class="button is-primary" />
</div>
</div>
</form>
<br><br>
{% if image_url %}
<p>File uploaded at: <a href="{{ image_url }}">{{ image_url }}</a></p>
{% endif %}
</div>
</div>
</body>
</html>
21 changes: 20 additions & 1 deletion django/upload/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
from django.conf import settings
from django.shortcuts import render
from django.core.files.storage import FileSystemStorage

# Create your views here.
from .models import Upload

def image_upload(request):
if request.method == 'POST':
image_file = request.FILES['image_file']
image_type = request.POST['image_type']
if settings.USE_S3:
upload = Upload(file=image_file)
upload.save()
image_url = upload.file.url
else:
fs = FileSystemStorage()
filename = fs.save(image_file.name, image_file)
image_url = fs.url(filename)
return render(request, 'upload.html', {
'image_url': image_url
})
return render(request, 'upload.html')

0 comments on commit 5a6b464

Please sign in to comment.