Git Product home page Git Product logo

Comments (8)

a13xg0 avatar a13xg0 commented on May 30, 2024 2

Regarding url method you might just get the URL and if your bucket is private you will get your presigned URL with valid time according to your settings MINIO_URL_EXPIRY_HOURS

from django-minio-backend.

theriverman avatar theriverman commented on May 30, 2024 1

@Podolyan98 you can get the url by accessing .file.url. in your case it would be either photo.file.url or license.file.url.
Using an example from DjangoExampleApplication:

from DjangoExampleApplication.models import PrivateAttachment
pa = PrivateAttachment.objects.first()
print(pa.file.url)
'https://play.min.io/xxxxx/2022-12-17/user/xxxx.png?X-Amz-Algorithm=(........)'

from django-minio-backend.

theriverman avatar theriverman commented on May 30, 2024

Thanks for stepping in, @a13xg0 !
I'm closing this now. If you're still experiencing problems, @ghorbani-mohammad, please open another issue!

from django-minio-backend.

Podolyan98 avatar Podolyan98 commented on May 30, 2024

@a13xg0 Hello. Through the Django admin panel the URL method works, but through the my API I only see the direct path:

GET http://localhost:8888/api/users/aa658d18-e0b0-4537-8cb6-7e3c2251c197

Response:

{
"id": "aa658d18-e0b0-4537-8cb6-7e3c2251c197",
"photo": "aa658d18-e0b0-4537-8cb6-7e3c2251c197/2022-12-16_11-34-01.png",
"license": "aa658d18-e0b0-4537-8cb6-7e3c2251c197/file.md",

How to get pre-signed url?

from django-minio-backend.

a13xg0 avatar a13xg0 commented on May 30, 2024

@Podolyan98 Hi! My first guess looking at your response, is that you just got the value from your DB, not the resolved URL thru the minio's backend. You may check the order of middleware in your config.

from django-minio-backend.

Podolyan98 avatar Podolyan98 commented on May 30, 2024

@a13xg0 How can I get the resolved url through the minio backend?

models.py:

photo = models.ImageField(storage=MinioBackend(bucket_name='django-backend-users-private', replace_existing=True), upload_to=user_directory_path, null=True, blank=True)
license = models.FileField(storage=MinioBackend(bucket_name='django-backend-users-private', replace_existing=True), upload_to=user_directory_path, null=True, blank=True)

settings.py:

MINIO_ENDPOINT = 'docs.domain.com'
MINIO_EXTERNAL_ENDPOINT = 'docs.domain.com'
MINIO_EXTERNAL_ENDPOINT_USE_HTTPS = True 
MINIO_ACCESS_KEY = '123'
MINIO_SECRET_KEY = '123'
MINIO_USE_HTTPS = True
MINIO_URL_EXPIRY_HOURS = timedelta(days=1)
MINIO_PRIVATE_BUCKETS = [
    'django-backend-dev-private',
    'django-backend-users-private',
]
MINIO_POLICY_HOOKS: List[Tuple[str, dict]] = []
MINIO_BUCKET_CHECK_ON_SAVE = True

from django-minio-backend.

Podolyan98 avatar Podolyan98 commented on May 30, 2024

@theriverman In models.py I specified the method that is used to get the private link in the APIView.

models.py:

photo_0 = models.ImageField(storage=MinioBackend(bucket_name='django-backend-users-private', replace_existing=True), upload_to=User_directory_path('photo_0'), null=True, blank=True)

@property
def photo_0_url(self):
    if self.photo_0 and hasattr(self.photo_0, 'url'):
        return self.photo_0.url

views.py:

def get(self, request, pk):
    try:
        queryset = User.objects.filter(id=pk).first()
        user_dict = queryset.values().first()
        # overwrite file path using resolved URL thru the minio's backend
        user_dict['photo_0'] = user.photo_0_url
        return Response(user_dict, status=status.HTTP_200_OK) 
    except Exception as error:
        logger.error(traceback.format_exc())
        raise APIException(error)

It works. The problem is that there are a lot of fields like ImageField and FileField, I need to overwrite each field in views.py, for example user_dict['photo_0'] = user.photo_0_url. Is it possible to call similar methods automatically for every ImageField or FileField?

By the way, when creating or updating records with ImageField and FileField, private links are automatically generated.

from django-minio-backend.

theriverman avatar theriverman commented on May 30, 2024

@Podolyan98 I don't see how your question is related to this third-party package.
What you're asking is more of a generic Django question which should not be discussed here.

from django-minio-backend.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.