Retrieve from Gridfs storage

102 views
Skip to first unread message

Anthony Eshleman

unread,
Nov 1, 2021, 7:53:19 PM11/1/21
to djongo
Ive gone ahead followed to structure for sending the files to gridfs storage on mongodb. I can't find a procedure for retrieving said files again and downloading them. Has anyone encountered this before and know how to solve this?

Dirk Holzhüter

unread,
Mar 16, 2022, 6:33:38 PM3/16/22
to djongo
I am currently having exactly the same issue! Does anyone has a solution? How do I return a file saved to the database using djongo/gridfs???

After sending the file to the db, everything is properly generated.
My model (just the auto generated id and the FileField) is saved in the collection "myModel".  I get a collection of "myfiles.files" and myfiles.chunks.
my django model looks like:

grid_fs_storage = GridFSStorage(collection='myfiles', base_url=''.join([settings.BASE_URL, 'myfiles/'])) 
class myModel(models.Model):
    myfile = models.FileField(storage=grid_fs_storage)
    class Meta:
        db_table = 'myModel'

I use drf-spectacular with multipart-parser to upload the file and get the following entry in the database:
myModel collection:   
 { "_id": {"$oid": "62325e25e9c2c70bf38c8842"}, "id": 1, "myfile": "testfile.txt" }

myfiles.files collection:
{ "_id": { "$oid": "62325e25e9c2c70bf38c8840" },
    "filename": "testfile.txt",
    "contentType": "text/plain",
    "md5": "23ab190c3786a60d569795b473e34683",
    "chunkSize": 261120,
    "length": { "$numberLong": "90" },
    "uploadDate": { "$date": "2022-03-16T22:01:09.071Z"}
}

my endpoint returns on the post request:
my view looks like:
class FileView(ListCreateAPIView):
    parser_classes = ( MultiPartParser,)
    serializer_class = myModelSerializer
    queryset = myModel.objects.all()
class myfiles(ListAPIView):
parser_classes = ( MultiPartParser,)
    serializer_class = myModelSerializer
    queryset = myModel.objects.all()
my urls.py looks like
... urlpatterns=[
re_path(r'^api/teststandards/file', api.FileView.as_view(), name='teststandard-file'),
    re_path(r'^myfiles', api.myfiles.as_view(), name='get-file'),
] I don't know how my view or the serializer should look like to return the file again?
How do I retrieve the file from the database with gridfs?

Any help is appreaciated. A small example to follow would be great!!!

regards

Dirk Holzhüter

unread,
Mar 25, 2022, 11:58:47 AM3/25/22
to djongo
Finally managed to get it to work...
This is how my views.py looks like:

#for upload
class FileView(ListCreateAPIView):
    parser_classes = ( MultiPartParser,)
    serializer_class = TestStandardFileSerializer
    queryset = TestStandardFile.objects.all()

# for download
class myfiles(RetrieveAPIView):
    parser_classes = ( MultiPartParser,)
    serializer_class = TestStandardFileSerializer
    queryset = TestStandardFile.objects.all()
   
    def retrieve(self, request, *args, **kwargs):
        obj = self.get_object()        
        response = HttpResponse(obj.myfile, content_type='application/octet-stream')
        response['Content-Disposition'] = 'attachment; filename=%s' % obj.myfile
        return response

...myfile is the name of the FileField
Reply all
Reply to author
Forward
0 new messages