有没有办法避免 mongo gridfs 中的重复文件?或者我必须通过应用程序代码来做到这一点(我正在使用 pymongo)
我有一个包含以下元数据的图像:
> db.fs.files.find().pretty()
{
"_id" : ObjectId("4576874577342672346"),
"chunkSize" : 262144,
"user_name" : "my name",
"filename" : "image.jpg",
"length" : 7103,
"uploadDate" : ISODate("2014-01-23T13:31:48.155Z"),
"user_email" : "email@email.com",
"md5" : "1234567890"
}
>
Run Code Online (Sandbox Code Playgroud)
我想从 Python (PyMongo) 中删除图像。
上的文档delete()似乎规定该delete()函数中唯一接受的参数是file_id:
http://api.mongodb.org/python/current/api/gridfs/#gridfs.GridFS.delete
以编程方式,我有以下可以在文件元数据中匹配的值:
我该如何:
file_id(必要时通过使用上述值)或 file_id?此外,我目前仅使用单个块文件进行测试,如果将来与更大的文件交互是否会删除file_id或其他元数据也会删除所有关联的块?
以下代码按预期工作:它读取已使用 multer [https://github.com/expressjs/multer] 上传的文件并将其流式传输到 gridfs。我也能够从 gridfs 中蒸出它。
var target = gfs.createWriteStream({
filename: fileItem.originalname,
mode: 'w',
metadata: metaData
});
fs.createReadStream(tempFile)
.on('end', function () {
console.log('File read and stored');
})
.on('error', function (err) {
console.log('Error handling file ' + err);
})
.pipe(target);
// Attach new file to document list
target.on('close', function (file) {
filesUploadedList.push(file._id);
Run Code Online (Sandbox Code Playgroud)
fileItem是通过迭代req.files.file上传的文件获得的。
但是,我正在尝试找出 multer 的 inMemory 选项。如果我将此设置为 true,fileItem则将填充缓冲区。请注意,如果没有此选项, 的缓冲区/内容fileItem是空的,这就是为什么在上面的代码中从fileItem.location.
用内容填充目标的最佳方法是什么?该fs.createReadStream做的那一刻管道。
谢谢。
J
我正在尝试在 nodejs 中编写一个程序,该程序在 mongodb 中存储文件列表。它工作正常,但有一个问题:它总是将 contentType 元数据存储为二进制/八位字节流,我希望它存储实际的 MIME 类型。我尝试在 readStream 之前获取 mime 类型(通过 promises),但即使我对 contentType 进行硬编码(例如“image/jpeg”),它也总是将元数据保存为“binary/octet-stream。
这是我的代码:
files.forEach(function(f) {
var conn = mongoose.createConnection(db);
conn.once('open', function () {
var gfs = Grid(conn.db);
var writeStream = gfs.createWriteStream({
filename: f.location,
mode: 'w',
contentType: 'image/jpeg'
});
writeStream.on('finish', function() {
console.log('Storing', f.location, 'OK');
return;
})
.on('error', function(data) {
console.log('Error', f.location, data)
});
fs.createReadStream(path.join(__dirname, 'files', f.location), 'utf-8')
.pipe(writeStream);
});
});
Run Code Online (Sandbox Code Playgroud)
任何的想法?谢谢您的反馈!
我们使用 PHP7,最新的 MongoDB PECL 包 (1.2.2) - Laravel 5.3 - jenssegers/laravel-mongodb 3.1
我想使用 GridFS。它通常在 MongoDB PECL 包中可用,但没有文档或工作代码示例。
我处理包含数字和文本的大型数据帧。显然我可以将每列/行存储为一个 sep。我的 mongoDB 中的文档,但我想在加载数据时删除和麻烦。
我考虑过使用 GridFS,它是通过使用 MongoEngine FileField 进行抽象的。与此同时,我想出了一个适合我的解决方案:
import pandas as pd
from mongoengine import Document, StringField, FileField
from io import BytesIO
class Frame(Document):
name = StringField(required=True, max_length=200, unique=True)
data = FileField()
@property
def frame(self):
str_data = BytesIO(self.data.read()).read().decode()
try:
return pd.read_json(str_data, typ="frame")
except ValueError:
return pd.read_json(str_data, typ="series")
def __str__(self):
return "{name}: \n{frame}".format(name=self.name, frame=self.frame)
def put(self, frame):
if self.data:
self.data.replace(frame.to_json().encode())
else:
self.data.new_file()
self.data.write(frame.to_json().encode())
self.data.close()
self.save()
if __name__ == '__main__':
from pydata.config import connect_production
connect_production()
frame = pd.DataFrame(data=[[1,2,4],[3,4,6]], columns=["A","B","C"], index=["X","Y"])
f …Run Code Online (Sandbox Code Playgroud) 使用 Jhipster 与 Spring+Mongo 和 Gridfs 处理保存在 db 中的文件。当我尝试上传大于 1Mb 的文件时,出现 500 错误:
java.io.IOException: UT000054: The maximum size 1048576 for an individual file in a multipart request was exceeded
Run Code Online (Sandbox Code Playgroud)
试图在 application-dev.yml 中设置它但没有成功:
spring:
http:
multipart:
max-file-size: 10MB
max-request-size: 10MB
Run Code Online (Sandbox Code Playgroud)
这个限制怎么改?
我使用 MongoDB 和 GridFS 来存储图像。我有这条路线从数据库检索图像:
\napp.get("/images/profile/:uId", function (req, res) {\nlet uId = req.params.uId;\nconsole.log(uId)\ngfs.files.findOne(\n {\n "metadata.owner": uId,\n "metadata.type": 'profile'\n }\n , function (err, file) {\n if (err || !file) {\n return res.status(404).send({ error: 'Image not found' });\n }\n var readstream = gfs.createReadStream({ filename: file.filename });\n readstream.on("error", function (err) {\n res.send("Image not found");\n });\n readstream.pipe(res);\n });\n});\nRun Code Online (Sandbox Code Playgroud)\n这会返回类似的内容:
\n\xef\xbf\xbdPNG\n\n\nIHDR\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd]\xef\xbf\xbdbKGD\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xcc\xbf pHYs\n\xef\xbf\xbd\n\xef\xbf\xbdB(\xef\xbf\xbdxtIME\xef\xbf\xbd -u\xef\xbf\xbd\xef\xbf\xbd~IDATx\xef\xbf\xbd\xef\xbf\xbdi|TU\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdJDV\xef\xbf\xbdfH\xef\xbf\xbd0\xef\xbf\xbd :-\nH_\xef\xbf\xbd\xef\xbf\xbdM\xef\xbf\xbd\xef\xbf\xbd03`\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\n(\xef\xbf\xbd\xef\xbf\xbd-\xef\xbf\xbdq{U[\xef\xbf\xbdm\xef\xbf\xbdA\xef\xbf\xbd\xef\xbf\xbdAQ\xef\xbf\xbdVZ\xef\xbf\xbd\xd2\xa2bP\xef\xbf\xbd\xef\xbf\xbdS\xef\xbf\xbd@K@\xef\xbf\xbd\xef\xbf\xbdCB\xef\xbf\xbd\xef\xbf\xbd|\xef\xbf\xbd\xef\xbf\xbdT\xef\xbf\xbd[\xef\xbf\xbd=\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd"U\xef\xbf\xbd\xef\xbf\xbd[\xef\xbf\xbd\xef\xbf\xbd{\xef\xbf\xbds\xef\xbf\xbd9\xef\xbf\xbd \n\xef\xbf\xbd+)@e\xdb\xbf\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd{\xef\xbf\xbd9\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd,?\xef\xbf\xbdT.S\xef\xbf\xbd\xef\xbf\xbdxL\xd6\x9fx&@\xef\xbf\xbd0TSFp7\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdt\xca\x81\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdA!_\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdD\xef\xbf\xbdh\xef\xbf\xbd \nz\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdod\xef\xbf\xbdG\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdYzV\xef\xbf\xbd?e\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd|\xef\xbf\xbdh\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd@P\xef\xbf\xbd,\xef\xbf\xbd{\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdZ\xef\xbf\xbdl\\vc\xef\xbf\xbdN\xd3\xb2\xef\xbf\xbd?\nn\xef\xbf\xbd\xef\xbf\xbd(\xef\xbf\xbdr\xef\xbf\xbd.......\nRun Code Online (Sandbox Code Playgroud)\n看来我正确地得到了 png 。那么如何在 img 标签中显示它呢?
\n添加新的分片时,我的MongoDB分片的群集接收性能无法提高。
我有一个小型集群设置,其中包含1个mongos + 1个配置副本集(3个节点)+ N个碎片副本集(每个3个节点)。
Mongos在一个专用的Kubernetes节点上,每个托管碎片的mongo进程都有其专用的k8s节点,而config mong进程在它们恰好部署于此的地方运行。
该群集主要用于GridFS文件托管,典型文件约为100Mb。
我正在使用1个,2个和3个分片进行压力测试,以查看它是否可以正确缩放,但不能缩放。
如果我用2个分片启动一个全新的集群并运行测试,它以(大约)两倍于我用1个分片的速度读取文件,但是如果我用1个分片启动了集群,则执行测试,然后再添加1个分片(总共2个分片),然后再次执行测试,摄取速度大约与之前使用1个分片的速度相同。
观察块的位置,当我立即使用2个分片启动集群时,负载在两个分片之间平均平衡。如果我从1个分片开始,并在一些插入之后添加第二个分片,那么这些块往往会全部放在旧的分片上,并且平衡器必须稍后将它们带到第二个分片。
要闻速览:
块大小1024 MB
分片键是GridFS file_id,已散列
嗨,我目前正在使用 Python Flask 来存储我的图像,我已经成功地使用 gridfs 将图像存储到 mongodb 中,但现在我不确定如何检索图像。
我知道由于文件很大,它将在 fs.chunks 中分成块。我可以查询结果,因为我为所有图像创建了一个 id 并将其附加到 fs.files,我将只使用 objectID 来查询我的 fs.chunks 集合。然而,在那之后我不知道如何显示图像。
有谁知道如何从 mongodb 的 gridfs 中为 python flask 检索图像。
gridfs ×10
mongodb ×9
python ×3
node.js ×2
flask ×1
html ×1
java ×1
jhipster ×1
kubernetes ×1
laravel-5 ×1
mime-types ×1
mongoengine ×1
pandas ×1
php ×1
png ×1
pymongo ×1
python-3.x ×1
sharding ×1
spring ×1