devpro-s3-storages
Django Project to handle Static and Upload Files on s3 the right way
How to use
Install it using your pacakage manager:
poetry add devpro-s3-storages
Configure your Django Settings to use those handlers:
AWS_STORAGE_BUCKET_NAME = '' # Empty means no s3 configuration so local filesystem will be used. Suitable for dev env
if AWS_STORAGE_BUCKET_NAME == '':
# Config to make static files locally
STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
# Config to make uploading/downloading files locally
MEDIA_ROOT = BASE_DIR / 'mediafiles'
MEDIA_URL = '/mediafiles/'
# Configuration to raise error in case ImageField/FileField does not define the property "upload_to" having
# "public/" or "private/" as prefix. See Model Usage to check those options
STORAGES = {
"default": {
"BACKEND": "devpro_s3_storages.handlers.FileSystemWithValidationStorage",
},
"staticfiles": {
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
},
}
else:
# Configuration to work using S3 bucket for static and file uploads/downloads
STATIC_URL = f'//{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/static/'
AWS_S3_ACCESS_KEY_ID = 'AWS_S3_ACCESS_KEY_ID'
AWS_S3_SECRET_ACCESS_KEY = 'AWS_S3_SECRET_ACCESS_KEY'
STORAGES = {
"default": {
"BACKEND": "devpro_s3_storages.handlers.S3FileStorage",
"OPTIONS": {
'default_acl': 'private',
'location': 'media',
},
},
"staticfiles": {
"BACKEND": "storages.backends.s3.S3Storage",
"OPTIONS": {
'default_acl': 'public-read',
'location': 'static',
'querystring_auth': False
},
},
}
So what this configuration will do when S3 bucket settings are provided?
Collect Static
Running python manage.py collectstatic will upload files to s3 with public-read permissions (Ensure to allow this on your S3 bucket configuration).
It will upload your static files under "static" folder in your bucket
It will generate unsigned urls for statics. Since static will be accessed on your site, they are already public, so why spending resource on creation signed urls for them?
File upload and download
When configuring ImageField/FileField in you models, you will need to define it the file will be public available or private. This will be setup using the "upload_to" property:
class PrivateFile(models.Model):
private_file = models.FileField(upload_to='private/right')
class PublicFile(models.Model):
public_file = models.FileField(upload_to='public/right')
So PrivateFile.private_file will be uploaded to bucket folder "media/private/right".
It will have private access, meaning only signed url will be able to access this content.
So PrivateFile.private_file.url() method will always generate a private signed url
On the other hand PublicFile.private_file will be uploaded to bucket folder "media/public/right".
It will have public access, meaning ounsigned url will be able to access this content.
So PublicFile.private_file.url() method will always generate a public url
Why this organization?
Some files are public, such as your avatar image in a social network. So it can better levarage cache and having a simple url.
Some file need privacy, such as some tax report you are collection. Si a signed url is needed for security puposes.
So this simple library delivery this kind of organization.
How to serve upload files locally?
The last step to having a proper local environment is making django to serv upload files when in dev environment.
So add this config to the final of your urls.py file:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns=[] # your mapped paths in here
# Add this path in case s3 bucket is not configured on settings
if settings.AWS_STORAGE_BUCKET_NAME == '':
urlpatterns.extend(static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT))
You can check a project example on https://github.com/devpro-br/devpro-s3-storages/tree/main/django_project_ex
Cheers!
Metadata
Release files for devpro-s3-storages 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| devpro_s3_storages-0.2.0.tar.gz | 3.8 kB | Without provenance | Details for devpro_s3_storages-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| devpro_s3_storages-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for devpro_s3_storages-0.2.0-py3-none-any.whl |
Total release size: 8.1 kB
Release files / devpro_s3_storages-0.2.0.tar.gz
| Download URL | devpro_s3_storages-0.2.0.tar.gz |
|---|---|
| Size | 3.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9a563887cb4ad4951134c48091dc6cdd89cbbccc30050a7353b3bb00ee544096
|
|
BLAKE2b-256 checksum How to use checksums |
776b222a8a02969bc30da110b37644e29c584839972118676464708a96e2ce5c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.12.5 Linux/5.15.153.1-microsoft-standard-WSL2
|
Release files / devpro_s3_storages-0.2.0-py3-none-any.whl
| Download URL | devpro_s3_storages-0.2.0-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9c0efa30b40315cb08ecade9bb138c33160bcc540f0810d1f2b81cdc45a1c2dd
|
|
BLAKE2b-256 checksum How to use checksums |
540a0fcd9f26898f5a4af17eea7dc6502a20fc722427638a16754c295d04da51
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.12.5 Linux/5.15.153.1-microsoft-standard-WSL2
|
Security for devpro-s3-storages 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.