Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate with django's custom file storage settings #187

Open
WhyNotHugo opened this issue May 31, 2023 · 1 comment
Open

Integrate with django's custom file storage settings #187

WhyNotHugo opened this issue May 31, 2023 · 1 comment

Comments

@WhyNotHugo
Copy link
Owner

We currently use a few custom settings (e.g.: AFIP_KEY_STORAGE, AFIP_CERT_STORAGE) to define storages.

Django 4.2 implements a standard mechanism for defining custom storages. We should move over to use that.

See: https://docs.djangoproject.com/en/4.2/releases/4.2/#custom-file-storages

@spuigdengolas
Copy link

spuigdengolas commented Nov 1, 2023

One example of custom storage backend could be the following. This setting makes files uploaded to S3 private.

storages.py

from django.core.files.storage import get_storage_class
from storages.backends.s3boto3 import S3Boto3Storage

class PrivateMediaStorage(S3Boto3Storage):
    location = "media/private"
    default_acl = "private"
    file_overwrite = False
    custom_domain = False

    def __init__(self):
        super().__init__()
        self.bucket_name = settings.AWS_PRIVATE_STORAGE_BUCKET_NAME

def select_private_storage():
    # important
    private_storage_class = get_storage_class(settings.PRIVATE_FILE_STORAGE)
    return private_storage_class()  # instantiate the storage

production.py

PRIVATE_FILE_STORAGE = "my_project.utils.storages.PrivateMediaStorage"

AFIP_PDF_STORAGE = PRIVATE_FILE_STORAGE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants