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(
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.