[英]Retrieve file metadata using python GridFS
I am accessing GridFS using Python, and would like both access to a file and its metadata. 我正在使用Python访问GridFS,并且希望访问文件及其元数据。
Python version is 2.7, MongoDB version is 3.0.7. Python版本是2.7,MongoDB版本是3.0.7。 OS is Ubuntu 14.04.
操作系统是Ubuntu 14.04。
The file is stored as follows: 该文件存储如下:
>>> fs = GridFS(db, "gridfstest")
>>> fs.put(
"HELLO WORLD",
test_metadata ="testing",
other_metadata="other"
)
And retrieved as follows: 并检索如下:
>>> retrieved_file = fs.find_one()
>>> retrieved_file.read()
b'HELLO WORLD'
>>> print(retrieved_file.metadata)
None
I was expecting .metadata to be a dictionary of metadata. 我期望.metadata是元数据的字典。
retrieved_file._file
stores the metadata I was looking for, along with additional metadata, but I assume accessing anything beginning with an underscore is a hack at best. retrieved_file._file
存储了我一直在寻找的元数据以及其他元数据,但是我认为以下划线开头的任何内容充其量都是hack。
So, how can I get the file and the metadata I originally set? 那么,如何获取最初设置的文件和元数据?
You metadata live in the files Collection which means in "gridfstest.files" so to retrieve your metadata you need to query that collection. 元数据位于文件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)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.