Can I change 'media' cache write function to use DO Spaces or AWS S3?

I would like my Kirby to store the files it creates in the ‘media’ directory on Digital Ocean Spaces (equivalent and works the same as AWS S3)

I understand how to modify the Kirby ‘root’ directories to look for the directories in a different directory, so I think I can handle that part. The part I don’t understand is how to modify where and how Kirby writes the cache files to.

To upload files to DO Spaces or S3 I need to use some parameters including a ‘key’ and ‘secret’. I can leverage the aws/aws-sdk-php library to do this from PHP, which will employ a method in the following form to do so:

require 'vendor/autoload.php';
use Aws\S3\S3Client;

$client = new Aws\S3\S3Client([
  'version' => 'latest',
  'region'  => 'us-east-1',
  'endpoint' => '',
  'credentials' => [
    'key'    => 'SPACES_KEY',
    'secret' => 'SPACES_SECRET',

So, I suppose the questions are:

[a] Is it possible Media::publish() method
[b] If yes, how do I override Media::publish() with the custom function above when Kirby creates media files for the cache?

Looking forward to any comments you may have, and thanks in advance :+1:

The file::version component is the place where you tell Kirby what to return when a thumb is requested:

The file::url component is the place where you tell Kirby where to find a file:

Other useful resources:

Thanks, @texnixe. I saw your Kirby loves CDN post, and I’m comfortable with the fetching files part, but what about sending the file to a different function (my custom function which POSTS to S3 / DOspaces) when Kirby creates those cache files.

i.e. Ideally I want Kirby to run that function at the point where it would normally create the cached file in the media directory

Do you think something like this would work, maybe, probably?

Kirby::plugin('author/cdn', [
    'components' => [
        'media::publish' => function ($kirby, $path, $options) {
           // a version of my custom function goes here

I can engineer my way around this by disabling the media cache and using page update hooks or whatever, but if I can override the default functionality, that’s probably a cleaner approach.

There is no such component.

The Media::publish() function is called inside the Media::link() function which in turn is called in some routes in /kirby/config/routes. These routes are loaded in the App class (api() method).

So I think you would probably have to override those routes and load your custom routes in a custom Kirby class, since you cannot overwrite these media routes.