diff --git a/docs/configuration/system.md b/docs/configuration/system.md index a1e0ebb17..ed588ccf3 100644 --- a/docs/configuration/system.md +++ b/docs/configuration/system.md @@ -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. diff --git a/netbox/netbox/configuration_example.py b/netbox/netbox/configuration_example.py index 84ead5339..5e1815930 100644 --- a/netbox/netbox/configuration_example.py +++ b/netbox/netbox/configuration_example.py @@ -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' diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 3a8e51e05..b99b43e66 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -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 "