我有一个使用 rails 5.1 开发的博客页面。一切正常,除了我在生产中创建帖子并附加图像后,图像会在一段时间后停止显示(比如 30 分钟)。我在互联网上搜寻解决方案并看到这表明问题与 Heroku 在每次应用程序重启后擦除目录有关。提供的一种解决方案是将您的图像托管在 Amazon S3 等服务上。
但是,我已经设置了 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"
..............................
我使用 Figaro gem 来屏蔽 env 文件。它们很好,因为 S3 存储桶响应了,而且我已经在博客上启动并运行了 OmniAuth。
这是它在图像的 chrome 控制台上显示的错误:
我真的需要帮助来启动和运行这个博客。感谢您的时间。