过程其实很简单。
第 1 步:
配置活动存储桶。尝试使用与您的carrierwave one不同的存储桶
第二步:
配置您的模型以提供对 ActiveStorage 的访问。例子
class Photo < AR::Base
mount_uploader :file, FileUploader # this is the current carrierwave implementation. Don't remove it
has_one_attached :file_new # this will be your new file
end
现在您将有两个相同模型的实现。载波访问 file 和 ActiveStorage file_new
第三步:
从 Carrierwave 下载图像并将其保存到活动存储中
这可以在 rake 文件、activeJob 等中实现。
Photo.find_each do |photo|
begin
filename = File.basename(URI.parse(photo.fileurl))
photo.file_new.attach(io: open(photo.file.url), filename: d.file )
rescue => e
## log/handle your errors in order to retry later
end
end
此时,您将在carrierwave 存储桶上拥有一张图像,在活动存储桶上拥有新创建的图像!
(可选)
第四步
一旦您准备好迁移,请修改您的模型,更改活动存储访问器并删除载波集成
class Photo < AR::Base
has_one_attached :file # we changed the atachment name from file_new to file
end
这是一个方便的选项,因此您在控制器和其他地方的集成保持不变。希望!
第 5 步
更新您在active_storage_attachments 表上的记录,以便将附件作为file 而不是file_new 更新列name 从“file_new”到“file”
注释
可以对应用程序进行一些其他调整以处理需要考虑的事情
- 如果您的网站将在您进行迁移时运行,则一种完全运行的方法是为新上传的内容实施主动存储,那么当您显示图像时,您可以将主动存储和carrierwave 显示为后备
helper 中的类似内容:
photo.attached? ? url_for(photo.file_new) : photo.file.url
我希望这会有所帮助!