Skip to content

Commit

Permalink
moved media and static storage from dropbox to backblaze #58
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdeathway committed Dec 3, 2023
1 parent b4a2c5c commit 17f4510
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
37 changes: 26 additions & 11 deletions archiver/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import os
from pathlib import Path
from .secgen import generate_secret_key
from archiver import is_available
from archiver import is_available,storage_backend
from dotenv import load_dotenv
load_dotenv()

Expand Down Expand Up @@ -157,10 +157,9 @@


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

MEDIA_ROOT=os.path.join(BASE_DIR,'media')
MEDIA_URL='/media/'


# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
Expand Down Expand Up @@ -204,18 +203,32 @@
},
'attachment_filesize_limit':FILE_SIZE_LIMIT,
'attachment_require_authentication': True,

}


DEFAULT_FILE_STORAGE = 'storages.backends.dropbox.DropBoxStorage'
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage" #"storages.backends.dropbox.DropboxStorage"
#DROPBOX_OAUTH2_TOKEN = os.environ.get('DROPBOX_OAUTH2_TOKEN')
DEFAULT_FILE_STORAGE = 'archiver.storage_backend.MediaStorage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
#dropbox settings
DROPBOX_ROOT_PATH = '/k9archiver/'
DROPBOX_APP_KEY = os.environ.get('DROPBOX_APP_KEY')
DROPBOX_APP_SECRET = os.environ.get('DROPBOX_APP_SECRET')
DROPBOX_OAUTH2_REFRESH_TOKEN=os.environ.get('DROPBOX_OAUTH2_REFRESH_TOKEN')

#[V-dev4.1.0] AWS/Backblaze settings for Media Storage
AWS_ACCESS_KEY_ID =os.environ.get('AWS_ACCESS_KEY_ID') #b2 application key id if using backblaze'
AWS_SECRET_ACCESS_KEY =os.environ.get('AWS_SECRET_ACCESS_KEY') #your b2 application key
AWS_STORAGE_BUCKET_NAME =os.environ.get('AWS_STORAGE_BUCKET_NAME') #'<a public bucket>'
AWS_S3_REGION_NAME =os.environ.get('AWS_S3_REGION_NAME') #'<your b2 region - e.g. us-west-001>'

AWS_S3_ENDPOINT = f's3.{AWS_S3_REGION_NAME}.backblazeb2.com'
AWS_S3_ENDPOINT_URL = f'https://{AWS_S3_ENDPOINT}'

AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}

STATIC_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.{AWS_S3_ENDPOINT}/"
#django debug toolbar and other settings for development
if DEBUG:
#if DEBUG is False then we are in production and we want to use postgres.
Expand All @@ -241,9 +254,11 @@
}

#django storages settings for development
#if USE_DROPBOX_IN_DEVELOPMENT is True then we will use dropbox in development
#it is to test dropbox functionality in development
USE_DROPBOX_IN_DEVELOPMENT=os.environ.get('USE_DROPBOX_IN_DEVELOPMENT', False) == 'True'
if not(USE_DROPBOX_IN_DEVELOPMENT):
#if USE_LOCAL_STORAGE is True then we will use local storage in development
USE_LOCAL_STORAGE=os.environ.get('USE_LOCAL_STORAGE', False) == 'True'
if USE_LOCAL_STORAGE:
STATIC_URL='/static/'
MEDIA_URL='/media/'
STATICFILES_STORAGE="whitenoise.storage.CompressedManifestStaticFilesStorage"
DEFAULT_FILE_STORAGE = 'django.core.files.storage.FileSystemStorage'

5 changes: 5 additions & 0 deletions archiver/storage_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from storages.backends.s3boto3 import S3Boto3Storage

class MediaStorage(S3Boto3Storage):
location = 'media'
file_overwrite = False
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ urllib3==2.0.7
webencodings==0.5.1
whitenoise==6.6.0
wrapt==1.13.3
boto3==1.33.6
10 changes: 8 additions & 2 deletions template.env
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@ DROPBOX_APP_KEY=""
DROPBOX_APP_SECRET=""
DROPBOX_OAUTH2_REFRESH_TOKEN=""

#for testing dropbox related functionality in development
USE_DROPBOX_IN_DEVELOPMENT=False
#for using local storage in development
USE_LOCAL_STORAGE=True

#AWS/BackBlaze configuration
AWS_ACCESS_KEY_ID =''
AWS_SECRET_ACCESS_KEY =''
AWS_STORAGE_BUCKET_NAME =''
AWS_S3_REGION_NAME=''

0 comments on commit 17f4510

Please sign in to comment.