Retrieving a MongoDB file using Python and Saving them to a folder

368 views
Skip to first unread message

Patricia Infante

unread,
Feb 21, 2017, 4:23:29 PM2/21/17
to mongodb-user
Hi! I am trying to retrieve a file (either executable, pdf, and any other type) and save it to a folder. I am planning to pass that file to another Python program but I don't know if I can call the file and pass it straight away from the db. So there, I am going to retrieve it then save in a folder, then call the file from that folder on the program.

Anyway, these files can be malicious, so I really am cautious on what script I'm going to write in retrieving it. Here is what I've done so far.

I tried to get it using GridFS.get() then set it as a file-like object using StringIO.StringIO (is this ok?). Then write that file to a folder. I am not yet testing this but this is the logic I have in mind.
    fs_sample = fs_db.get(sampleId)
   
#Get the file using GridFS's get()
   
#After getting a sample object, get the file-like object via StringIO
    sample_file
= StringIO.StringIO(fs_sample.read())
       
   
#Pass the sample as file object to method
    thug_sample_path
= os.path.join(TASK_DIR, 'analysis/samples') #where I'm going to save the file
    fileOut
= open(thug_sample_path, "w")
    fileOut
.write(fs_sample)
    fileOut
.close(

Any help will be appreciated.

Wan Bachtiar

unread,
Feb 24, 2017, 12:29:08 AM2/24/17
to mongodb-user

Hi Patricia,

As per your example, you can retrieve a file from GridFS and write it out into a file :

fs_sample = fs.get(sampleId)
fileOut = open("/path/analysis/samples/file.pdf", "w")
fileOut.write(fs_sample.read())
fileOut.close()

See more GridFS example for more information.

If you would like to analyse the content of the file in memory using StringIO , this would be a decision based on your application/system use case and requirements.

Anyway, these files can be malicious, so I really am cautious on what script I’m going to write in retrieving it.

MongoDB as the database back-end of your application handles the storing and the retrieving of the data. If this question is related more to the security design of your Python application, I would recommend to post a question on StackOverflow for wider audience.

Regards,

Wan.

Reply all
Reply to author
Forward
0 new messages