Open1

carrierwaveでcloudflareのR2に画像をアップロードする設定

さもじるさもじる

fog-awsでやる方法はわからなかった
carrierwave-awsで、ドキュメントのUsageからコピーしてきてそれっぽい設定をしたらとりあえず動いた

CarrierWave.configure do |config|
  config.storage    = :aws
  config.aws_bucket = Rails.application.credentials.dig(:cloudflare, :r2_bucket)
  config.aws_acl    = 'public-read'

  config.asset_host = 'https://example.com' #R2に設定した独自ドメイン

  config.aws_authenticated_url_expiration = 60 * 60 * 24 * 7

  config.aws_attributes = lambda {
    {
      expires: 1.week.from_now.httpdate,
      cache_control: 'max-age=604800',
    }
  }

  config.aws_credentials = {
    access_key_id: Rails.application.credentials.dig(:cloudflare, :r2_access_key_id),
    secret_access_key: Rails.application.credentials.dig(:cloudflare, :r2_secret_access_key),
    region: 'auto',
    endpoint: "https://#{Rails.application.credentials.dig(:cloudflare, :r2_account_id)}.r2.cloudflarestorage.com",
    stub_responses: Rails.env.test?,
  }
end

https://github.com/carrierwaveuploader/carrierwave-aws