How to override base url when creating links to public files?
You are basically describing the use case of a CDN (Content Delivery Network).
Check CDN module:

This module provides easy Content Delivery Network integration for Drupal sites. It alters file URLs, so that files are downloaded from a CDN instead of your web server.
It provides two modes: &quot;Origin Pull&quot; and &quot;File Conveyor&quot;.

In Origin Pull mode, only &quot;Origin Pull&quot; CDNs are supported. (These are CDNs that only require you to replace the domain name with another domain name.)
In File Conveyor mode, this module integrates with File Conveyor, which allows for much more advanced setups (e.g. optimize images, transcode videos, and so on, before syncing to the CDN).