In my case I need to ensure the uniqueness of files via SHA1 (stored as the filename
)
db = pymongo.MongoClient('localhost', 27017).test
gfs = gridfs.GridFS(db)
# How may I create a unique index in GridFS?
gfs.files.create_index([('filename', 1)], unique=True)
And find the file by SHA1 if the file has already been stored.
sha1 = hashlib.sha1(file_content).hexdigest()
try:
return gfs.put(file_content, filename=sha1)
except pymongo.errors.DuplicateKeyError:
# How may I find files via criterion?
return gfs.find( { 'filename': sha1 } )['_id']
Could anybody tell me how to do those things? Thanks in advance.