简体   繁体   English

从 MongoDB 加载图像缓慢

[英]Slow image loading from MongoDB

I'm creating a small MERN stack app (using Atlas database) where users can upload multiple images and then view them in some sort of gallery.我正在创建一个小型 MERN 堆栈应用程序(使用 Atlas 数据库),用户可以在其中上传多个图像,然后在某种画廊中查看它们。

Currently, I upload images using GridFS.目前,我使用 GridFS 上传图像。

The problem is that when I try to get for instance a single image from the database it takes too much time until I get a response: about a second for a small 200x200 picture when usually it takes 50 ms or even less.问题是,当我尝试从数据库中获取单个图像时,需要花费太多时间才能得到响应:对于 200x200 的小图片大约需要一秒钟,而通常需要 50 毫秒或更短的时间。

Here is my route for getting an image:这是我获取图像的途径:

router.get('/photo/single/:filename', (req, res) => {
  const bucket = new mongoose.mongo.GridFSBucket(mongoose.connection.db, {
    chunkSizeBytes: 1024,
    bucketName: 'uploads'
  })

  bucket
    .openDownloadStreamByName(req.params.filename).pipe(res)
})

Not much code here as you can see.如您所见,这里的代码不多。

So why does openDownloadStreamByName work so slow in my case?那么为什么openDownloadStreamByName在我的情况下工作如此缓慢? Is it just because I'm using a free M0 cluster or I'm just missing something in my route?是因为我正在使用免费的 M0 集群还是我只是在我的路线中遗漏了一些东西? Or maybe storing images in MongoDB is not a good idea by itself (but I don't see another solution since in my case images can weight more that 16MB)?或者,在 MongoDB 中存储图像本身并不是一个好主意(但我没有看到其他解决方案,因为在我的情况下,图像的权重可能超过 16MB)?

I gave up on the idea of storing files in MongoDB.我放弃了在 MongoDB 中存储文件的想法。 Instead, I've created a bucket on AWS S3 and it works fine.相反,我在 AWS S3 上创建了一个存储桶,并且运行良好。

嗨,请使用GridFS在 MongoDB Atlas 中存储图像和其他二进制数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM