下面是使用download_to_stream_by_name和創建磁盤上的一個目錄中的文件的一個例子:
import os
from pymongo import MongoClient
from gridfs import GridFSBucket
my_db = MongoClient().test
fs = GridFSBucket(my_db)
file_id = fs.upload_from_stream(
"test_file",
"data I want to store!",
chunk_size_bytes=4,
metadata={"contentType": "text/plain"})
fs = GridFSBucket(my_db)
# Get file to write to
if not os.path.exists('my_directory'):
os.makedirs('my_directory')
file = open('my_directory/myfile','wb')
fs.download_to_stream_by_name("test_file", file)
A「類文件對象」是一個有需要的字符塊的「寫」的方法寫。由「打開」功能創建的實際文件對象是文件類對象的一種可能性。
請注意,傳遞給download_to_stream_by_name的文件名是GridFS中的文件名,而不是磁盤上的文件名。
@Rawing我多次閱讀文檔,但絕對沒有關於文件類對象應該是什麼的澄清。類似於文件的對象,就像它在文檔示例中似乎沒有提供任何機會來指定存儲位置,即使該參數被稱爲「目標」。 –