Created
January 25, 2017 07:44
-
-
Save tim-hub/d2c2a60379073e48b916e097aac81995 to your computer and use it in GitHub Desktop.
mongodb use gridfs pymongo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import pymongo | |
import gridfs | |
# establish a connection to the database | |
connection = pymongo.MongoClient() | |
#get a handle to the test database | |
db = connection.test | |
file_meta = db.file_meta | |
file_used = "sample_128_mb.txt" | |
def main(): | |
grid = gridfs.GridFS(db, "text") | |
fin = open(file_used, "r") | |
_id = grid.put(fin) | |
fin.close() | |
print "id of file is", _id | |
file_meta.insert( { "grid_id": _id, "filename": file_used}) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment