为mongodb网站expressjs存储图像的最佳方法是哪一种? [英] Which is the best way to store images for expressjs, mongodb site?

查看:54
本文介绍了为mongodb网站expressjs存储图像的最佳方法是哪一种?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新快递,nodejs,mongodb.我正在建立一家服装店的网站.他们希望经常上传图像,也许每两天就会上传新图像,并且这些图像需要快速显示在网站上,即缩略图.单击图像将显示产品的详细信息,等等.

我的问题是存储图像的最佳方法是什么?

  1. 将图像存储到硬盘中,然后将绝对路径存储到数据库mongodb中吗?
  2. 将图像以二进制/base64格式存储到数据库mongodb中吗?

如何将图像快速加载到网站中?

谢谢

解决方案

我们在哪里存储图像?

MongoDB中的解决方案#1

因此,第一个解决方案是将图像存储在MongoDB内部.它可以容纳图像文件或任何文件类型.因此,您可以获取文件并将其绑定到MongoDB内部的记录,然后将其直接保存到数据库中.

通过这种方法,可以将特定服装的描述页面与其相应的图像绑定在一起变得容易,因为您可以将该图像直接嵌入到该页面的开发中,并且客户会对该方法感到满意,因为当用户检索到该图像时,图片随附该服装的详细说明页面.

这就是一种可能的解决方案,只需拍摄图像并将其直接存储到MongoDB中即可.

但是,我建议这是一种不好的方法.这样做的原因是,您可以告诉客户是否存在回退,因为他们通常会根据其Mongo副本使用的存储量来为Mongo实例付费.

因此,他们用尽的存储空间越多,他们每月支付的费用就越多.

例如,上一次我检查MLab的使用时,他们每GB收取15美元.因此,托管1GB图像所需的客户费用为$ 15.

对于另一个电子商务网站,我们正在谈论3GB简易存储,它或多或少地转换为330张图像,平均每月15美元.

因此,如果他们的项目经理中的一位每天上载一件新衣服,我们将很快谈论巨额费用.

因此,我个人认为直接在MongoDB中存储任何类型的文件确实不是一种选择,因为这会导致非常昂贵的成本.

这只是一种可能的解决方案.

连接到服务器的高清解决方案#2

因此,让我们看一下您可能会使用的第二种解决方案.您可能会使用捆绑在Express服务器上的硬盘驱动器.因此,当将此应用程序部署到诸如Heroku,Digital Ocean,Linode或AWS的某些云环境中时,通常会获得一个与应用程序关联的硬盘驱动器.

因此,也许您要拍摄图像并将其放置在本地硬盘驱动器中.这种方法是绝大多数在线帖子和文章所倡导的:

因此,上述体系结构的问题是,如果映像被保存到两个硬盘驱动器之一中,然后稍后出现访问该映像的请求,但是想象该请求被路由到另一个Express服务器.不存在该图像的其他硬盘驱动器.

当您开始使用服务提供商(例如Linode或Digital Ocean)时,就会出现此问题,在服务器和硬盘驱动器之间具有一对一的映射.

如果这是您现在所需要的,这是一个短期解决方案,但是一旦该应用程序开始扩展,它将成为一个问题.

外部数据存储区中的解决方案#3

第三种解决方案是我过去在React with Node应用程序和Ruby on Rails应用程序中使用的解决方案.实际上,我在Ruby on Rails产品组合网站上使用的是该解决方案,它位于Heroku平台上.

因此,当图像上传后,而不是Express API试图像本地硬盘上一样在本地存储文件,它将获取图像并使用外部数据存储来保存应用程序中所有不同的图像./p>

我在我的投资组合网站上使用的东西以及我在带有React应用程序的Node上使用的东西是Amazon S3,但同时也存在Azure File Storage和Google Cloud Storage.这些系统可容纳大量数据,并且可以是您可能想到的任何类型的文件.不仅是您所需要的图像,还包括视频文件,音频文件等.

使用S3可以拥有的存储量没有上限,但是您不必使用S3,但是它现在被视为行业标准,但是您可以轻松地使用Azure和Google云.

我认为您的客户会喜欢这种解决方案的好处,Amazon S3每月向您收取每千兆字节2便士的存储费用.

I'm new express, nodejs, mongodb. I'm building a website for a clothing shop. They want to upload images frequently, maybe every 2 days they will upload new images, and those images need to be shown on the website quickly, thumbnails. Click on the image will show detail the product, etc.

My question is what are the best ways to store images?

  1. Store images into hard disk and just store the absolute path into database mongodb?
  2. Stored images as binary/base64 into database mongodb?

How to load images into the website quickly?

Thanks

解决方案

Where do we store images?

Solution # 1 in MongoDB

So the first solution is to store the images inside of MongoDB. It can accommodate image files or any file type. So you can take a file and tie it to a record inside of MongoDB and save it directly to your database.

With this approach tying a description page of a particular article of clothing with its corresponding image becomes easy because you can embed this image directly into your the development of that page and your client would be happy with that approach because when the user retrieves a detailed description page of that article of clothing it comes with the image.

So thats one possible solution, just take the image and store directly into MongoDB.

However, I am going to suggest that this is a bad approach. The reason for that, you can tell your client if there is pushback is that they will usually pay for their Mongo instance in terms of the amount of storage that their Mongo copy uses.

So all the more storage they use up, they pay more money per month.

For example, the last time I checked the use of MLab, they were charging $15 per GB. So that's $15 out of your customers pocket for hosting 1GB worth of images.

For yet another e-commerce website, we are talking about 3GB easy which translates into 330 images more or less, equally $15 a month.

So if one of their project managers is uploading a new article of clothing once a day, we are talking about a tremendous cost very quickly.

So, personally I think that storing any kind of file type directly inside of MongoDB is really not an option because it will get very costly.

So that's just one possible solution.

Solution # 2 in HD attached to server

So let's look at a second solution that might be available to you. You might use a hard drive thats tied your Express server. So when this application gets deployed to some cloud environment like Heroku, Digital Ocean, Linode or AWS you usually get a hard drive associated with your application.

So maybe you take the images and place them inside the local hard drive. This approach is what the vast majority of online posts and articles will advocate for:

How to upload, display and save images using node.js and express

https://appdividend.com/2019/02/14/node-express-image-upload-and-resize-tutorial-example/

https://medium.com/@nitinpatel_20236/image-upload-via-nodejs-server-3fe7d3faa642

Just with the three I gathered above you have a pretty robust blueprint to get started with.

Every single one says take the file and save it to your local hard drive. In this particular article:

https://alligator.io/nodejs/uploading-files-multer-express/

they are showing this code:

const storage = multer.diskStorage({
  destination: 'some-destination',
  filename: function (req, file, callback) {
    //..
  }
});

They are using the image upload library called multer which provides the diskStorage() engine for uploading images to disk.

So this is one approach that the development community at-large agrees with.

This is a good approach in the context of a one-to-one mapping.

The problems with this approach starts to arise when we have multiple machines.

An example of this is if you have multiple machines hosted on Digital Ocean or Linode where each environment is a separate instance.

If you have all your images stored in the accompanying hard drive and then you start to scale your server up, they would each have their own separate hard drive.

So you may have a request that comes in through a load balancer and the load balancer decides where to send the request to like the diagram below:

So the issue with the above architecture is if the image gets saved to one of the two hard drives and then later on a request comes in to access that same image, but imagine the request gets routed to the other Express server with a different hard drive where the image does not exist.

This is a problem that comes up when you start to use a service provider like Linode or Digital Ocean where you have a one-to-one mapping between server and hard drive.

It's a short term solution if that is all you need for now, but once that application starts to scale its going to be a problem.

Solution # 3 outside data store

This third solution is one I have used in the past with React with Node applications and Ruby on Rails applications as well. In fact, my Ruby on Rails portfolio website uses this solution and it sits on the Heroku platform.

So when the image gets uploaded, rather than the Express API trying to store the file locally like on its own hard drive, it will take the image and use an outside data store to hold all the different images from the app.

The one I use for my portfolio website and what I have used for Node with React applications has been Amazon S3, but there also exists Azure File Storage and Google Cloud Storage. These systems are made to hold a tremendous amount of data and they can be any type of file that you can possibly imagine. Not just images such as in your case, but video files, audio files, etcetera.

There is no cap to the amount of storage you can have with S3, but you don't have to use S3, but it is seen as an industry standard right now, but you can easily just as well use Azure and Google Cloud.

The benefit of this solution that I think your customer will appreciate, Amazon S3 charges you like two pennies per gigabyte per month for storage.

这篇关于为mongodb网站expressjs存储图像的最佳方法是哪一种?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆