Skip to content

Commit 6f01da9

Browse files
committed
Closes #20206: Clarifies django-storages configuration from env vars
1 parent bf73564 commit 6f01da9

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

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 "Common Gotcha"
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)