Simulate paperclip s3_prefixes_in_alias option

Paperclip had the option s3_prefixes_in_alias (https://www.rubydoc.info/gems/paperclip/Paperclip/Storage/S3)

s3_prefixes_in_alias : The number of prefixes that is prepended by s3_host_alias. This will remove the prefixes from the path in :s3_alias_url url interpolation

Our setup was a CDN host images.domain.com which mapped to an s3 bucket with a images/ prefix.

I would like to reproduce the same setup, such that shrine uploads to the s3 bucket with a prefix, but the url host is the CDN host without the images/ prefix.

Hi, there isn’t an equivalent in Shrine at the moment, as I hadn’t considered this use case.

I’m not sure how to cleanly add this functionality to Shrine at the moment, but for now you could monkey-patch the #url method:

module S3HostPrefix
  def url(*, host: nil, **)
    if host
      uri = URI.parse(super)
      uri.path = uri.path.match(/^\/#{prefix}/).post_match
      uri.to_s
    else
      super
    end
  end
end

Shrine::Storage::S3.prepend S3HostPrefix

Thanks, this works well!