1
在我的情况下,通过索引/查询我需要确保文件的通过SHA1的独特性(存储为filename
)pymongo - 如何创建GridFS的
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)
并通过SHA1找到该文件,如果文件中有已经存储。
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']
有人能告诉我如何做这些事吗?提前致谢。
如果这个代码在并发环境中运行?我需要数据库来确保唯一性。 – neuront