0

这是我的第一个带有Refile gem的 Rails 项目,所以我还没有完全掌握这个美妙的文件上传 gem 的所有细节。

我正在尝试配置 Refile gem,以便在生产模式下将文件上传到 Amazon S3,但在开发模式下将文件上传到文件系统。

我的 config/initializers/refile.rb 文件如下所示:

require "refile/backend/s3"

aws = {
  access_key_id: Rails.application.secrets.s3_key, 
  secret_access_key: Rails.application.secrets.s3_secret, 
  bucket: "adlit",
}
Refile.cache = Refile::Backend::S3.new(prefix: "cache", **aws)
Refile.store = Refile::Backend::S3.new(prefix: "store", **aws)
end

但是当我尝试在开发中上传图像时,我收到以下错误消息:

Missing Credentials. Unable to find AWS credentials. You can configure your     AWS credentials a few different ways: * Call AWS.config with :access_key_id and     :secret_access_key * Export AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to ENV *...

因此,我尝试通过在条件中添加配置来解决此问题:

if Rails.env.production
  ...
end

但这并没有解决它,我仍然在开发模式下收到上述错误消息。

有谁知道我如何配置 Refile 以在生产模式下将文件上传到 Amazon S3 并在开发模式下将文件上传到文件系统?

感谢您的帮助,

安东尼

4

2 回答 2

0

我已经做到了这一点,它对我来说很好。

我注意到你有一个小错字,你错过?了 env 检查。它应该是:

Rails.env.production?

这能解决你的问题吗?

于 2015-02-24T12:15:17.323 回答
0

我认为您可以将 Amazon S3 的所有配置代码放在 config/environments/production.rb 中。

于 2015-04-16T06:42:23.903 回答