【问题标题】:Rake task for migrating from ActiveStorage to Shrine从 ActiveStorage 迁移到 Shrine 的 Rake 任务
【发布时间】:2021-08-28 20:34:58
【问题描述】:
我有一个使用 ActiveStorage 和 S3 的 Rails 5.2 应用程序,但我遇到了间歇性超时问题。我也对其他应用程序的神殿感到更自在。
我一直在尝试创建一个 rake 任务来遍历所有带有 ActiveStorage 附件的记录并将它们作为 Shrine 附件重新上传,但我遇到了一些问题。
我尝试通过 URL 和 tempfiles 执行此操作,但我不确定获取 activestorage 版本并将其上传到 S3 并作为神社附件保存在记录中的正确步骤。
我已经尝试了 rake 任务 here,但我认为该方法仅适用于 rails 6。
有什么提示或建议吗?
【问题讨论】:
标签:
amazon-s3
ruby-on-rails-5
rails-activestorage
shrine
【解决方案1】:
我确信这不是最有效的,但它确实有效。
task :listing_images => :environment do
listings = Listing.all
listings.each do |listing|
if listing.exterior.attached?
# fetch file and save to tempfile
tempfile = Tempfile.new([listing.exterior.filename.base.to_s, listing.exterior.filename.extension_with_delimiter.to_s], Rails.root.join('tmp'))
tempfile.binmode
listing.exterior.send(:download_blob_to, tempfile)
# upload to S3 through shrine and save on column
uploader = ImageUploader.new(:store)
uploaded_file = uploader.upload(File.open(tempfile.path))
listing.update_columns(exterior_image_data: uploaded_file.to_json)
# generate different image sizes
generate_derivatives(listing.reload, view)
tempfile.close!
end
end
end