1

we am using django rest framework for a project and here we are trying to avoid using serializers. We have our AWS S3 as private , hence we need to provide a presigned key for the user to be able to see the image . Initially , we had our S3 as public so we were using annotate function to convert the image path to image link like this :

def getImageFormatter(key:str)->Func:
"""Function to Get Formatted URL"""
return Case(
    When(
        **{f'{key}__exact':''},
        then=Value(None)
        ),
    When(
        **{f'{key}__isnull':False},
        then=Concat(
            Value(settings.MEDIA_ROOT),
            F(key),output_field=CharField()
        )
    ),
    default=Value(None),
    output_field=CharField(null=True)
    )

The problem is , this only works for public S3 . For private how can we do so so that we can get image on annotate? like using a custom django db function or something else? Any leads will be appreciated .

The database we are using is PostgreSQL. We are using boto3 and django-storages to connect to our s3 bucket.

We are actually not trying to run it through a serializer , and we are getting the data directly by using annotate and values.

unownone
  • 56
  • 1
  • 6
  • 1
    Related: https://stackoverflow.com/questions/64193171/how-to-generate-presigned-s3-urls-using-django-storages – jarmod Jun 08 '22 at 13:48
  • @jarmod hi , the question does talk about generating urls , but my issue is to generate it in such a way so that i can get the final url in the queryset.values() result . – unownone Jun 08 '22 at 15:46

0 Answers0