Skip to content

Commit d172e62

Browse files
authored
Merge pull request #20323 from netbox-community/20206-document-env-var-config-approach
#20206: Clarify `django-storages` configuration from env vars
2 parents d1e4028 + cd122a7 commit d172e62

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

docs/configuration/data-validation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ CUSTOM_VALIDATORS = {
1717
},
1818
"my_plugin.validators.Validator1"
1919
],
20-
"dim.device": [
20+
"dcim.device": [
2121
"my_plugin.validators.Validator1"
2222
]
2323
}

docs/configuration/system.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,46 @@ The specific configuration settings for each storage backend can be found in the
257257
!!! note
258258
Any keys defined in the `STORAGES` configuration parameter replace those in the default configuration. It is only necessary to define keys within the `STORAGES` for the specific backend(s) you wish to configure.
259259

260+
### Environment Variables and Third-Party Libraries
261+
262+
NetBox uses an explicit Python configuration approach rather than automatic environment variable detection. While this provides clear configuration management and version control capabilities, it affects how some third-party libraries like `django-storages` function within NetBox's context.
263+
264+
Many Django libraries (including `django-storages`) expect to automatically detect environment variables like `AWS_STORAGE_BUCKET_NAME` or `AWS_S3_ACCESS_KEY_ID`. However, NetBox's configuration processing prevents this automatic detection from working as documented in some of these libraries.
265+
266+
When using third-party libraries that rely on environment variable detection, you may need to explicitly read environment variables in your NetBox `configuration.py`:
267+
268+
```python
269+
import os
270+
271+
STORAGES = {
272+
'default': {
273+
'BACKEND': 'storages.backends.s3.S3Storage',
274+
'OPTIONS': {
275+
'bucket_name': os.environ.get('AWS_STORAGE_BUCKET_NAME'),
276+
'access_key': os.environ.get('AWS_S3_ACCESS_KEY_ID'),
277+
'secret_key': os.environ.get('AWS_S3_SECRET_ACCESS_KEY'),
278+
'endpoint_url': os.environ.get('AWS_S3_ENDPOINT_URL'),
279+
'location': 'media/',
280+
}
281+
},
282+
'staticfiles': {
283+
'BACKEND': 'storages.backends.s3.S3Storage',
284+
'OPTIONS': {
285+
'bucket_name': os.environ.get('AWS_STORAGE_BUCKET_NAME'),
286+
'access_key': os.environ.get('AWS_S3_ACCESS_KEY_ID'),
287+
'secret_key': os.environ.get('AWS_S3_SECRET_ACCESS_KEY'),
288+
'endpoint_url': os.environ.get('AWS_S3_ENDPOINT_URL'),
289+
'location': 'static/',
290+
}
291+
},
292+
}
293+
```
294+
295+
This approach works because the environment variables are resolved during NetBox's configuration processing, before the third-party library attempts its own environment variable detection.
296+
297+
!!! warning "Configuration Behavior"
298+
Simply setting environment variables like `AWS_STORAGE_BUCKET_NAME` without explicitly reading them in your configuration will not work. The variables must be read using `os.environ.get()` within your `configuration.py` file.
299+
260300
---
261301

262302
## TIME_ZONE

0 commit comments

Comments
 (0)