使用 python GridFS 检索文件元数据
Retrieve file metadata using python GridFS
我正在使用 Python 访问 GridFS,并且想要访问文件及其元数据。
Python版本为2.7,MongoDB版本为3.0.7。 OS 是 Ubuntu 14.04.
文件存储如下:
>>> fs = GridFS(db, "gridfstest")
>>> fs.put(
"HELLO WORLD",
test_metadata ="testing",
other_metadata="other"
)
并检索如下:
>>> retrieved_file = fs.find_one()
>>> retrieved_file.read()
b'HELLO WORLD'
>>> print(retrieved_file.metadata)
None
我原以为 .metadata 是元数据字典。 retrieved_file._file
存储我正在寻找的元数据以及其他元数据,但我认为访问以下划线开头的任何内容充其量只是一种黑客行为。
那么,我怎样才能得到我原来设置的文件和元数据呢?
您的元数据存在于 files Collection 中,这意味着在 "gridfstest.files" 中,因此要检索您的元数据,您需要查询该集合。
In [54]: col = db.gridfstest.files.findOne()
In [55]: col.find_one()
Out[55]:
{'_id': ObjectId('5644e9220acf451b36f22438'),
'chunkSize': 261120,
'encoding': 'utf8',
'length': 11,
'md5': '361fadf1c712e812d198c4cab5712a79',
'other_metadata': 'other',
'test_metadata': 'testing',
'uploadDate': datetime.datetime(2015, 11, 12, 19, 31, 46, 175000)}
我正在使用 Python 访问 GridFS,并且想要访问文件及其元数据。
Python版本为2.7,MongoDB版本为3.0.7。 OS 是 Ubuntu 14.04.
文件存储如下:
>>> fs = GridFS(db, "gridfstest")
>>> fs.put(
"HELLO WORLD",
test_metadata ="testing",
other_metadata="other"
)
并检索如下:
>>> retrieved_file = fs.find_one()
>>> retrieved_file.read()
b'HELLO WORLD'
>>> print(retrieved_file.metadata)
None
我原以为 .metadata 是元数据字典。 retrieved_file._file
存储我正在寻找的元数据以及其他元数据,但我认为访问以下划线开头的任何内容充其量只是一种黑客行为。
那么,我怎样才能得到我原来设置的文件和元数据呢?
您的元数据存在于 files Collection 中,这意味着在 "gridfstest.files" 中,因此要检索您的元数据,您需要查询该集合。
In [54]: col = db.gridfstest.files.findOne()
In [55]: col.find_one()
Out[55]:
{'_id': ObjectId('5644e9220acf451b36f22438'),
'chunkSize': 261120,
'encoding': 'utf8',
'length': 11,
'md5': '361fadf1c712e812d198c4cab5712a79',
'other_metadata': 'other',
'test_metadata': 'testing',
'uploadDate': datetime.datetime(2015, 11, 12, 19, 31, 46, 175000)}