- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
从 ActiveStorage 开始,您可以知道定义用于存储文件的镜像。
local:
service: Disk
root: <%= Rails.root.join("storage") %>
amazon:
service: S3
access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
region: us-east-1
bucket: mybucket
mirror:
service: Mirror
primary: local
mirrors:
- amazon
- another_mirror
最佳答案
我有几个可能适合您的解决方案,一个适用于 Rails <= 6.0,另一个适用于 Rails >= 6.1:
首先,您需要遍历 ActiveStorage Blob:
ActiveStorage::Blob.all.each do |blob|
# work with blob
end
然后...
local_file = ActiveStorage::Blob.service.primary.path_for blob.key
# I'm picking the first mirror as an example,
# but you can select a specific mirror if you want
mirror = blob.service.mirrors.first
mirror.upload blob.key, File.open(local_file), checksum: blob.checksum
如果镜像已存在,您可能还想避免上传文件。你可以这样做:mirror = blob.service.mirrors.first
# If the file doesn't exist on the mirror, upload it
unless mirror.exist? blob.key
# Upload file to mirror
end
把它放在一起,一个 rake 任务可能看起来像:# lib/tasks/active_storage.rake
namespace :active_storage do
desc 'Ensures all files are mirrored'
task mirror_all: [:environment] do
# Iterate through each blob
ActiveStorage::Blob.all.each do |blob|
# We assume the primary storage is local
local_file = ActiveStorage::Blob.service.primary.path_for blob.key
# Iterate through each mirror
blob.service.mirrors.each do |mirror|
# If the file doesn't exist on the mirror, upload it
mirror.upload(blob.key, File.open(local_file), checksum: blob.checksum) unless mirror.exist? blob.key
end
end
end
end
您可能会遇到类似 @Rystraum mentioned 的情况。您可能需要从本地磁盘以外的其他地方进行镜像。在这种情况下,rake 任务可能如下所示:# lib/tasks/active_storage.rake
namespace :active_storage do
desc 'Ensures all files are mirrored'
task mirror_all: [:environment] do
# All services in our rails configuration
all_services = [ActiveStorage::Blob.service.primary, *ActiveStorage::Blob.service.mirrors]
# Iterate through each blob
ActiveStorage::Blob.all.each do |blob|
# Select services where file exists
services = all_services.select { |file| file.exist? blob.key }
# Skip blob if file doesn't exist anywhere
next unless services.present?
# Select services where file doesn't exist
mirrors = all_services - services
# Open the local file (if one exists)
local_file = File.open(services.find{ |service| service.is_a? ActiveStorage::Service::DiskService }.path_for blob.key) if services.select{ |service| service.is_a? ActiveStorage::Service::DiskService }.any?
# Upload local file to mirrors (if one exists)
mirrors.each do |mirror|
mirror.upload blob.key, local_file, checksum: blob.checksum
end if local_file.present?
# If no local file exists then download a remote file and upload it to the mirrors (thanks @Rystraum)
services.first.open blob.key, checksum: blob.checksum do |temp_file|
mirrors.each do |mirror|
mirror.upload blob.key, temp_file, checksum: blob.checksum
end
end unless local_file.present?
end
end
end
虽然第一个 rake 任务回答了 OP 的问题,但后者更加通用:blob.mirror_later
将其包装为 rake 任务如下所示:# lib/tasks/active_storage.rake
namespace :active_storage do
desc 'Ensures all files are mirrored'
task mirror_all: [:environment] do
ActiveStorage::Blob.all.each do |blob|
blob.mirror_later
end
end
end
关于ruby-on-rails - 如何同步新的 ActiveStorage 镜像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52684443/
我正在将一个权限敏感的应用程序迁移到 ActiveStorage。我需要确保文件只能由具有权限的人访问,并且这些权限只能持续指定的时间段。 使用 Paperclip,这就像使用专家授权调用定义路由一样
我想知道 ActiveRecord 模型对象上的图像是否已更改,以便我可以执行其他功能,但看不到任何可用的信息。 IE。 object.image.changed? or object.image_c
我计划在我的一个网站中将 Rails 升级到 5.2 并引入 ActiveStorage,现在我使用 Paperclip 和 paperclip_optimizer。不利的一面是,当用 ActiveS
在我们的 Rails 代码中没有任何名为 default 的队列。但似乎 Sidekiq 将 ActiveStorage::PurgeJob 的队列设置为 default。这就是为什么 purge_l
我有一个表单,用户可以在其中选择要上传的文件。一旦使用 ActiveStorage 直接上传选择了文件,就会上传该文件。有一个提交按钮,在上传完成之前不可点击。 该按钮在表单加载时被禁用。但是,如果由
我有一个基本的 ActiveStorage 设置,其中一个型号为 has_many_attached :file_attachments .在其他地方的服务中,我试图生成一个在主应用程序(电子邮件、工
我有一个简单的 Controller 规范,它应该在创建项目时检查新 Material 的处理是否排队。 materials_controller_spec.rb(#create): it 'qu
我使用 url 方法在 erb 文件中呈现我的事件记录附件的 url。 #controller class RecordMetadataController " width="600" heig
我有一个在 Heroku 上运行的 Rails 6 应用程序,用于捕获视频并上传它们。一个典型的用例是用户制作视频然后上传。为此,我成功地使用了 active storage 并直接上传到 Amazo
我需要给图片加水印,cmd ImageMagick 命令 convert image.png watermark.png -composite target.jpg 或 composite -wate
我使用Rails + ActiveStorage 上传图片文件,上传后想将宽高保存在数据库中。但是,我无法在任何地方找到任何此类示例。 这是我从各种 API 文档中拼凑出来的,但最终还是出现了这个错误
每当尝试附加文件时,我的应用程序(本地)都会引发ActiveStorage::IntegrityError错误。我如何摆脱这个错误? 我只有一个has_one_attached,但我不知道该错误是如何
我将ActiveStorage与默认磁盘服务一起使用。我的api返回图像URL,并且图像通过IMG标签嵌入在前端(Vue.js)中。当我打开图片网址时,图片会正确显示。 render json: {
有没有办法使用 ActiveStorage 验证附件?例如,如果我想验证内容类型或文件大小? 类似 Paperclip 的方法会很棒! validates_attachment_content_t
我正在将 Rails 应用程序从回形针迁移到 ActiveStorage,但它不接受 crop变体中的参数 这一行: @user.image.variant(crop: [180,135]) 导致此错
我没有看到任何关于此事的文件,但要详细说明: 我想使用事件存储在我的 rails 应用程序中上传文件,而不必使用浏览器的文件输入元素。无论是使用拖放,还是各种自定义文件选择器,最好告诉 ActiveS
我在 ActiveStorage 中遇到这个问题,我需要处理图像,我的要求是保存处理过的图像并在裁剪和其他转换后将其附加到新模型。 最佳答案 ActiveStorage::Blob#variant 适
我目前正在将一个 Rails 应用程序(驱动 screenshots.debian.net)从 Paperclip 迁移到 ActiveStorage。它允许用户上传 PNG 图像,然后向所有用户显示
目标 我想上传多个文件。因此,一个Intervention 可以有多个Uploads,每个Upload 都有一个附加文件。这样一来,每个Upload 都可以附加一个具有不同状态、名称、可见性等的文件,
使用 Rails 5.2 rc1,是否可以在开发中直接上传到本地主机? 这是我的表单new.html.erb: 这是我在控制台中得到的错误: ActiveSupport::Messag
我是一名优秀的程序员,十分优秀!