Ruby-on-rails 我可以在 heroku 中托管图像吗?还是我需要S3?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24642967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:46:57  来源:igfitidea点击:

Can I host images in heroku? Or do I need S3?

ruby-on-railsherokuamazon-s3

提问by Gibson

I'm deploying my web app (it's for a corporate client). So, users will not add images, but only the business will.

我正在部署我的 Web 应用程序(用于公司客户)。因此,用户不会添加图像,而只有企业会添加。

I've deployed to Heroku, and my images are still showing. When do I need to use S3? Ill have like 100 images in total in the site, and size will vary like > 7 a week. Can I use only heroku?

我已经部署到 Heroku,我的图像仍在显示。我什么时候需要使用 S3?网站上总共有 100 张图片,大小会有所不同,每周超过 7 张。我可以只使用heroku吗?

回答by fivedigit

The short answer: if you allow users or admins to upload images, you should not use Heroku's file system for this as the images will suddenly vanish.

简短的回答:如果您允许用户或管理员上传图像,则不应为此使用 Heroku 的文件系统,因为图像会突然消失。

As explained in the Heroku documentation:

Heroku 文档中所述

Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno's lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted.

每个 dyno 都有自己的临时文件系统,以及最近部署的代码的新副本。在 dyno 的生命周期内,其正在运行的进程可以使用文件系统作为临时暂存器,但任何其他 dyno 中的进程都看不到写入的文件,并且在 dyno 停止或重新启动时,写入的任何文件都将被丢弃。

This means that user uploaded images on the Heroku filesystem are not only wiped out with every push, but also with every dyno restart, which occasionally happens (even if you would ping them frequently to prevent them going to sleep).

这意味着用户上传到 Heroku 文件系统上的图像不仅会在每次推送时被清除,而且在每次 dyno 重启时也会被清除,这种情况偶尔会发生(即使您会频繁 ping 它们以防止它们进入睡眠状态)。

Once you start using a second web dyno, it will not be able to read the other dyno's filesystem, so then images would only be visible from one dyno. This would cause weird issues where users can sometimes see images and sometimes they don't.

一旦您开始使用第二个 web dyno,它将无法读取另一个 dyno 的文件系统,因此图像只能从一个 dyno 中可见。这会导致奇怪的问题,用户有时可以看到图像,有时看不到。

That said, you can temporarily store images on the Heroku filesystem if you implement a pass-through file upload to an external file store.

也就是说,如果您实现了传递文件上传到外部文件存储,您可以临时将图像存储在 Heroku 文件系统

回答by Richard Peck

Asset Pipeline

资产管道

FiveDigit's answer is very good - there is something more to consider; the role of the asset pipelinein Rails

FiveDigit's answer非常好 - 还有更多的东西需要考虑;asset pipelineRails 中的作用

If the images you have are used as assets(IE they are used in the layout; are not changeable by the user), then you can store them in the assets/imagesfolder. There is no limit to the number of assets you can keep with your application, but you must be sure on what these are - they are files which aid your application's operation; notfiles which can be uploaded / manipulated:

如果您拥有的图像用作资产(即它们在布局中使用;用户不能更改),那么您可以将它们存储在assets/images文件夹中。您可以在应用程序中保留的资产数量没有限制,但您必须确定这些是什么——它们是帮助您的应用程序运行的文件;不是可以上传/操作的文件:

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages and pre-processors such as CoffeeScript, Sass and ERB.

资产管道提供了一个框架来连接和缩小或压缩 JavaScript 和 CSS 资产。它还增加了用其他语言和预处理器(如 CoffeeScript、Sass 和 ERB)编写这些资产的能力。

The asset pipeline will compress & fingerprint the stylesheet, imageand jsfiles it has, when you deploy your application to the likes of Heroku, or any other server. This means if those files don't change, you can store them in there

当您将应用程序部署到 Heroku 或任何其他服务器时stylesheet,资产管道将压缩和指纹它拥有的,imagejs文件。这意味着如果这些文件没有改变,你可以将它们存储在那里

-

——

S3

S3

The reason you'd want to use the likes of S3is specifically if your images files are designed to change (user can upload / edit them). Regardless of Heroku's filesystem, if the images are tied to changes in the DB, you'll have to keep a central store for them - if you change servers, they need to be reachable

您想要使用之类的原因S3特别是如果您的图像文件旨在更改(用户可以上传/编辑它们)。无论 Heroku 的文件系统如何,如果图像与数据库中的更改相关联,您必须为它们保留一个中央存储 - 如果您更改服务器,它们需要可以访问

To do this, you should ensure you appreciate how you want the files to work - are they going to be manipulated constantly by the user or not? If so, you'll have to explore integrating S3into your app

要做到这一点,您应该确保您了解您希望文件如何工作——它们是否会被用户不断地操作?如果是这样,您将不得不探索集成S3到您的应用程序中