我有一个使用 Rails 5.1 开发的博客页面。一切工作正常,除了在我在制作中创建帖子并附加图像后,图像在一段时间(例如 30 分钟)后停止显示。我在互联网上寻找解决方案并看到this https://shrinerb.com/rdoc/classes/Shrine/Storage/FileSystem.html这表明问题与 Heroku 在每次应用程序重新启动后擦除目录有关。提供的一种解决方案是将图像托管在 Amazon S3 等服务上。
I have however set S3 up and the images are being sent to the bucket shown below:
但博客文章图像仍然消失。我需要帮助,因为我无法弄清楚我缺少什么。以下是相关代码:
神社.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
gemfile:
....................................
# 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 存储桶有响应,而且我已经在博客上启动并运行 OmniAuth。
这是在 chrome 控制台上显示的图像错误:
我真的需要帮助来建立和运行这个博客。感谢您的时间。