Skip to content

Instantly share code, notes, and snippets.

@L04DB4L4NC3R
Created September 6, 2019 00:13
Show Gist options
  • Select an option

  • Save L04DB4L4NC3R/a81be4f8ea80bb8839cd5e7d5030d56d to your computer and use it in GitHub Desktop.

Select an option

Save L04DB4L4NC3R/a81be4f8ea80bb8839cd5e7d5030d56d to your computer and use it in GitHub Desktop.
get files
from pymongo import MongoClient
import gridfs
import base64
import sys
import bson
import json
import os.path
save_path = "./files"
fs_file = sys.argv[1]
db = MongoClient("YOUR_DB_URI").test
fs = gridfs.GridFS(db)
filemeta = []
with open(fs_file) as f:
data = bson.decode_all(f.read())
for i in data:
filemeta.append({"_id": i["_id"], "filename": i["filename"]})
print(filemeta[-1])
fileData = fs.get(i["_id"]).read()
completeName = os.path.join(save_path, i["filename"])
ff = open(completeName, "w")
ff.write(fileData)
ff.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment