Extend STORAGE_BACKEND config to support Swift

Requires django-storage-swift >= 1.4.0 when used.

Bug: T310717
Change-Id: I67cf439e9152608cbba3a3de4173d54ba5fbddc2
This commit is contained in:
Arzhel Younsi 2023-12-06 11:35:39 +01:00 committed by Arzhel Younsi
parent f4532dd4ab
commit a79a67ab7b
3 changed files with 34 additions and 6 deletions

View File

@ -177,7 +177,7 @@ The dotted path to the desired search backend class. `CachedValueSearchBackend`
Default: None (local storage)
The backend storage engine for handling uploaded files (e.g. image attachments). NetBox supports integration with the [`django-storages`](https://django-storages.readthedocs.io/en/stable/) package, which provides backends for several popular file storage services. If not configured, local filesystem storage will be used.
The backend storage engine for handling uploaded files (e.g. image attachments). NetBox supports integration with the [`django-storages`](https://django-storages.readthedocs.io/en/stable/) and [`django-storage-swift`](https://github.com/dennisv/django-storage-swift) packages, which provides backends for several popular file storage services. If not configured, local filesystem storage will be used.
The configuration parameters for the specified storage backend are defined under the `STORAGE_CONFIG` setting.
@ -187,7 +187,7 @@ The configuration parameters for the specified storage backend are defined under
Default: Empty
A dictionary of configuration parameters for the storage backend configured as `STORAGE_BACKEND`. The specific parameters to be used here are specific to each backend; see the [`django-storages` documentation](https://django-storages.readthedocs.io/en/stable/) for more detail.
A dictionary of configuration parameters for the storage backend configured as `STORAGE_BACKEND`. The specific parameters to be used here are specific to each backend; see the [`django-storages` documentation](https://django-storages.readthedocs.io/en/stable/) or [`django-storage-swift` documentation](https://github.com/dennisv/django-storage-swift) for more detail.
If `STORAGE_BACKEND` is not defined, this setting will be ignored.

View File

@ -230,6 +230,17 @@ SESSION_FILE_PATH = None
# 'AWS_STORAGE_BUCKET_NAME': 'netbox',
# 'AWS_S3_REGION_NAME': 'eu-west-1',
# }
# Swift is also supported as storage backend, for example:
# STORAGE_BACKEND = 'swift.storage.SwiftStorage'
# STORAGE_CONFIG = {
# 'SWIFT_AUTH_URL': 'https://swift.netbox.dev/auth/v1.0',
# 'SWIFT_USERNAME': 'netbox',
# 'SWIFT_PASSWORD': 'Secret',
# 'SWIFT_CONTAINER_NAME': 'netbox',
# 'SWIFT_USE_TEMP_URLS': True,
# 'SWIFT_TEMP_URL_KEY': 'Secret',
# 'SWIFT_BASE_URL': 'https://demo.netbox.dev/',
# }
# Time zone (default: UTC)
TIME_ZONE = 'UTC'

View File

@ -206,6 +206,11 @@ DATABASES = {
if STORAGE_BACKEND is not None:
DEFAULT_FILE_STORAGE = STORAGE_BACKEND
def _setting(name, default=None):
if name in STORAGE_CONFIG:
return STORAGE_CONFIG[name]
return globals().get(name, default)
# django-storages
if STORAGE_BACKEND.startswith('storages.'):
try:
@ -219,12 +224,24 @@ if STORAGE_BACKEND is not None:
raise e
# Monkey-patch django-storages to fetch settings from STORAGE_CONFIG
def _setting(name, default=None):
if name in STORAGE_CONFIG:
return STORAGE_CONFIG[name]
return globals().get(name, default)
storages.utils.setting = _setting
elif STORAGE_BACKEND == 'swift.storage.SwiftStorage':
try:
import swift.utils # type: ignore
except ModuleNotFoundError as e:
if getattr(e, 'name') == 'swift':
raise ImproperlyConfigured(
f"STORAGE_BACKEND is set to {STORAGE_BACKEND} but django-storage-swift is not present. "
"It can be installed by running 'pip install django-storage-swift'."
)
raise e
# Monkey-patch django-storage-swift to fetch settings from STORAGE_CONFIG
swift.utils.setting = _setting
if STORAGE_CONFIG and STORAGE_BACKEND is None:
warnings.warn(
"STORAGE_CONFIG has been set in configuration.py but STORAGE_BACKEND is not defined. STORAGE_CONFIG will be "