theriverman / django-minio-backend

Minio Backend for Django
https://pypi.org/project/django-minio-backend/
MIT License
112 stars 22 forks source link

Download url not working #40

Closed deviirmr closed 1 year ago

deviirmr commented 1 year ago

Hi, If the bucket is public then the download URL from the Django admin is working well, but if the bucket is private then the URL is not working. image

here is the URL pattern I received when I click on admin items

https://yourdomain.com/dmc/2023-2-7/dddddd.tif?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=xxxxxxxx/20230207/us-east-1/s3/aws4_request&X-Amz-Date=20230207T085042Z&X-Amz-Expires=432000&X-Amz-SignedHeaders=host&X-Amz-Signature=d42c4c9980ba06393192e1b1bdc2b3ab2011be447424f3c064e51d5a48e37898

it is also strange that the download URL passes the credential as it is :(

Does anyone try the download option for the private bucket?

FatihZor commented 1 year ago

Hello,

The credentials you specified contain only the username. I personally don't think this will be a problem, that's how I use it myself.

URLs created for private buckets are valid for 7 days by default. You can do this for a shorter time in the settings.py file.

MINIO_URL_EXPIRY_HOURS = timedelta(minutes=15) # Default is 7 days (longest) if not defined

If there is more than one private bucket in settings.py, you can specify it in get_storage_class and model.

settings.py

MINIO_PRIVATE_BUCKETS = [
    'private',
    'reports',
]

report_model.py

from django.db import models

from django_minio_backend import MinioBackend, iso_date_prefix
from django.core.files.storage import get_storage_class

media_storage = get_storage_class()(bucket_name='reports')

class ReportModel():

    title = models.CharField(max_length=250, 
                blank=False, 
                null=False)

    file = models.FileField(verbose_name="File", 
                storage=MinioBackend(bucket_name='reports'),
                upload_to=iso_date_prefix, 
                blank=False, 
                null=False)

    def __str__(self):
        return self.title

    @property
    def file_path(self):
        return media_storage.url(name=self.file.file.name)

Note: I am using the file_path property that I wrote when calling this model in views or template. Thus, the newest link created by minio for this file always comes.

In addition, if you share the response when you click on the link, I will try to help more.

theriverman commented 1 year ago

Thanks for chipping in, @FatihZor ! @deviirmr please see the following reference implementation for a model using a private bucket: https://github.com/theriverman/django-minio-backend/blob/abeed73bb039a5566858c3390660946ce5b09b9b/DjangoExampleApplication/models.py#L94-L134

Pay attention to the declared bucket name: https://github.com/theriverman/django-minio-backend/blob/abeed73bb039a5566858c3390660946ce5b09b9b/DjangoExampleApplication/models.py#L132

This bucket name must be added to the MINIO_PRIVATE_BUCKETS list in your settings too.