diff --git a/docs/configuration/system.md b/docs/configuration/system.md index d1924f75b..d0144551b 100644 --- a/docs/configuration/system.md +++ b/docs/configuration/system.md @@ -220,17 +220,21 @@ STORAGES = { Within the STORAGES dict, "default" is used for image uploads and "scripts" is used for Scripts. -The configuration parameters for the specified storage backend are defined under the `STORAGE_CONFIG` setting. +If using a remote storage like S3, define the config as STORAGES[key]["OPTIONS"] for each storage item as needed. For example: ---- +```python +STORAGES = { + "scripts": { + "BACKEND": "storages.backends.s3boto3.S3Boto3Storage", + "OPTIONS": { + 'access_key': 'access key', + 'secret_key': 'secret key', + } + }, +} +``` -## STORAGE_CONFIG - -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 documentation for your selected backend ([`django-storages`](https://django-storages.readthedocs.io/en/stable/) or [`django-storage-swift`](https://github.com/dennisv/django-storage-swift)) for more detail. - -If `STORAGE_BACKEND` is not defined, this setting will be ignored. +The specific configuration settings for each storage can be found in the [django-storages documentation](https://django-storages.readthedocs.io/en/latest/index.html). --- diff --git a/netbox/core/models/files.py b/netbox/core/models/files.py index 79c79f191..e6427c71a 100644 --- a/netbox/core/models/files.py +++ b/netbox/core/models/files.py @@ -93,7 +93,7 @@ class ManagedFile(SyncedDataMixin, models.Model): Write the object's data to disk at the specified path """ # Check whether file already exists - storage = self.get_storage() + storage = self.get_storage if storage.exists(path) and not overwrite: raise FileExistsError() @@ -122,7 +122,7 @@ class ManagedFile(SyncedDataMixin, models.Model): def delete(self, *args, **kwargs): # Delete file from disk - storage = self.get_storage() + storage = self.get_storage try: storage.delete(self.full_path) except FileNotFoundError: diff --git a/netbox/extras/models/mixins.py b/netbox/extras/models/mixins.py index 2fc9f3e47..f22b32004 100644 --- a/netbox/extras/models/mixins.py +++ b/netbox/extras/models/mixins.py @@ -26,17 +26,6 @@ class CustomStoragesLoader(importlib.abc.Loader): exec(code, module.__dict__) -def load_module(module_name, filename): - spec = importlib.util.spec_from_file_location(module_name, filename) - if spec is None: - raise ModuleNotFoundError(f"Could not find module: {module_name}") - loader = CustomStoragesLoader(filename) - module = importlib.util.module_from_spec(spec) - sys.modules[module_name] = module - loader.exec_module(module) - return module - - class PythonModuleMixin: def get_jobs(self, name): @@ -64,5 +53,16 @@ class PythonModuleMixin: return name def get_module(self): - module = load_module(self.python_name, self.name) + """ + Load the module using importlib, but use a custom loader to use django-storages + instead of the file system. + """ + spec = importlib.util.spec_from_file_location(self.python_name, self.name) + if spec is None: + raise ModuleNotFoundError(f"Could not find module: {self.python_name}") + loader = CustomStoragesLoader(self.name) + module = importlib.util.module_from_spec(spec) + sys.modules[self.python_name] = module + loader.exec_module(module) + return module