Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Heroku、Shire和Amazon S3:博客帖子图像在一段时间后消失_Heroku_Amazon S3_Shrine - Fatal编程技术网

Heroku、Shire和Amazon S3:博客帖子图像在一段时间后消失

Heroku、Shire和Amazon S3:博客帖子图像在一段时间后消失,heroku,amazon-s3,shrine,Heroku,Amazon S3,Shrine,我有一个使用rails 5.1开发的博客页面。一切都很好,除了我在制作中创建一个帖子并附加一个图像后,图像在一段时间后停止显示(比如30分钟)。我在互联网上四处寻找解决方案,发现问题与Heroku每次重启应用程序后都会清除目录有关。提供的一个解决方案是将您的图像托管在类似AmazonS3的服务上 不过,我已经设置了S3,图像正在发送到如下所示的存储桶: 但是,博客帖子的图片仍然消失了。我需要帮助,因为我不知道我错过了什么。以下是相关代码: 神殿。rb: require "shrine" req

我有一个使用rails 5.1开发的博客页面。一切都很好,除了我在制作中创建一个帖子并附加一个图像后,图像在一段时间后停止显示(比如30分钟)。我在互联网上四处寻找解决方案,发现问题与Heroku每次重启应用程序后都会清除目录有关。提供的一个解决方案是将您的图像托管在类似AmazonS3的服务上

不过,我已经设置了S3,图像正在发送到如下所示的存储桶:

但是,博客帖子的图片仍然消失了。我需要帮助,因为我不知道我错过了什么。以下是相关代码:

神殿。rb:

require "shrine"
require "shrine/storage/s3"
s3_options = {
    access_key_id:      ENV['S3_KEY'],
    secret_access_key:  ENV['S3_SECRET'],
    region:             ENV['S3_REGION'],
    bucket:             ENV['S3_BUCKET'],
}

if Rails.env.development?
  require "shrine/storage/file_system"
  Shrine.storages = {
    cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"), # temporary
    store: Shrine::Storage::FileSystem.new("public", prefix: "uploads/store")  # permanent
  }
elsif Rails.env.test?
  require 'shrine/storage/memory'
  Shrine.storages = {
    cache: Shrine::Storage::Memory.new,
    store: Shrine::Storage::Memory.new
  }
else
  require "shrine/storage/s3"

  Shrine.storages = {
    cache: Shrine::Storage::S3.new(prefix: "cache", **s3_options),
    store: Shrine::Storage::S3.new(prefix: "store", **s3_options)
  }
end
Shrine.plugin :activerecord # or :activerecord
Shrine.plugin :cached_attachment_data # for retaining the cached file across form redisplays
....................................
# A rich text editor for everyday writing
gem 'trix', '~> 0.11.1'
# a toolkit for file attachments in Ruby applications
gem 'shrine', '~> 2.11'
# Tag a single model on several contexts, such as skills, interests, and awards
gem 'acts-as-taggable-on', '~> 6.0'
# frameworks for multiple-provider authentication.
gem 'omniauth-facebook'
gem 'omniauth-github'
# Simple Rails app key configuration
gem "figaro"
..............................
gemfile:

require "shrine"
require "shrine/storage/s3"
s3_options = {
    access_key_id:      ENV['S3_KEY'],
    secret_access_key:  ENV['S3_SECRET'],
    region:             ENV['S3_REGION'],
    bucket:             ENV['S3_BUCKET'],
}

if Rails.env.development?
  require "shrine/storage/file_system"
  Shrine.storages = {
    cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"), # temporary
    store: Shrine::Storage::FileSystem.new("public", prefix: "uploads/store")  # permanent
  }
elsif Rails.env.test?
  require 'shrine/storage/memory'
  Shrine.storages = {
    cache: Shrine::Storage::Memory.new,
    store: Shrine::Storage::Memory.new
  }
else
  require "shrine/storage/s3"

  Shrine.storages = {
    cache: Shrine::Storage::S3.new(prefix: "cache", **s3_options),
    store: Shrine::Storage::S3.new(prefix: "store", **s3_options)
  }
end
Shrine.plugin :activerecord # or :activerecord
Shrine.plugin :cached_attachment_data # for retaining the cached file across form redisplays
....................................
# A rich text editor for everyday writing
gem 'trix', '~> 0.11.1'
# a toolkit for file attachments in Ruby applications
gem 'shrine', '~> 2.11'
# Tag a single model on several contexts, such as skills, interests, and awards
gem 'acts-as-taggable-on', '~> 6.0'
# frameworks for multiple-provider authentication.
gem 'omniauth-facebook'
gem 'omniauth-github'
# Simple Rails app key configuration
gem "figaro"
..............................
我使用Figaro gem来屏蔽env文件。它们很好,因为S3 bucket会响应,而且我已经在博客上启动并运行了OmniAuth

以下是chrome控制台上显示的图像错误:


我真的需要帮助来建立和运行这个博客。谢谢您的时间。

默认情况下,Shieline会生成过期的S3 URL,因此生成的URL可能会以某种方式被缓存,一旦URL过期,图像就变得不可用

作为一种解决方法,您可以将S3上传设置为公共的,并生成公共URL。您可以告诉S3存储将上载公开(注意,这只会影响新上载,现有上载将保持私有,因此您必须以另一种方式将其公开),并在默认情况下通过更新初始值设定项生成公共URL:

# ...

require "shrine/storage/s3"

Shrine.storages = {
  cache: Shrine::Storage::S3.new(prefix: "cache", upload_options: { acl: "public-read" }, **s3_options),
  store: Shrine::Storage::S3.new(prefix: "store", upload_options: { acl: "public-read" }, **s3_options)
}

# ...

Shrine.plugin :default_url_options, cache: { public: true }, store: { public: true }

# ...

图像是否也从AWS S3中删除?如果您设置了自动过期
cache/*
图像,我建议您再次查看它,以检查您是否只针对
cache/*
目录。您还可以查看是否可以为S3 bucket启用日志记录,以及何时何地向您的bucket发出删除请求。这是非常奇怪的,神社不只是自动删除存储中的文件,只有当你删除记录时,这会立即发生。@janko-m,这些图像在AWS上根本不会被删除。他们只是停止在博客页面上加载。显示一个带有“x”标记的小图标视图,以删除它,就像您在损坏的文件中看到的一样。只需补充一点,我在撰写博客时使用trix编辑器进行拖放。在本地机器上一切正常。它只有在生产中才会中断。也许这与Hicle生成的S3URL默认过期有关?它们是否以某种方式缓存在您的应用程序中?@janko-m,这可能是一个原因。如果是这样的话,你知道我怎样才能阻止这种事情发生吗?我不是这些东西的专家。但我愿意学习。今年一月才开始编码。不要因为我的问题而拖延;)我一直收到这个错误,config/initializers/sike.rb:12:in
”:未初始化的常量sike::Storage::S3(NameError)from/home/odogwudozilla/.rvm/gems/ruby-2.4.1/gems/railties-5.1.6/lib/rails/engine.rb:655:in
block-in-load\u-config\u-initializer'from/home/odogwudozilla/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.6/lib/active\u-support/notifications.rb.168:in
instrument'from/home/odogwudozilla/.rvm/gems/ruby-2.4.1/gems/railties-5.1.6/lib/rails/engine.rb:654:in
load_-config_-initializer'…对最后的评论表示歉意。我无意中评论了我的愚蠢。谢谢@janko-m。你的解决方案对我很有效。就在今天晚上,我意识到你是神殿宝石的主人。有一次,我在网上的几次涉及神社的搜索中不断碰到同一个“janko”。你的热情参与是非常值得称赞的,当使用该选项时,我得到了拒绝访问的错误。我应该在AWS中更改什么以便允许上载公共读取对象?