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

**URL:** <https://forum.getkirby.com/t/can-i-change-media-cache-write-function-to-use-do-spaces-or-aws-s3/23829>\
**Category:** Questions\
**Tags:** v3\
**Created:** [November 1, 2021, 4:00pm UTC](https://forum.getkirby.com/t/can-i-change-media-cache-write-function-to-use-do-spaces-or-aws-s3/23829 "2021-11-01T16:00:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon-hayden](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/simon-hayden/32/4362_2.png) [@simon-hayden](https://forum.getkirby.com/u/simon-hayden)\
**Post date:** [November 1, 2021, 4:00pm UTC](https://forum.getkirby.com/t/can-i-change-media-cache-write-function-to-use-do-spaces-or-aws-s3/23829/1 "2021-11-01T16:00:27Z")

</div>

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`](https://github.com/aws/aws-sdk-php) library to do this from PHP, which will employ a method in the following form to do so:

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

$client = new Aws\S3\S3Client([
  'version' => 'latest',
  'region' => 'us-east-1',
  'endpoint' => 'https://nyc3.digitaloceanspaces.com',
  'credentials' => [
    'key' => 'SPACES_KEY',
    'secret' => 'SPACES_SECRET',
  ],
]);

```

So, I suppose the questions are:

[a] Is it possible [Media::publish()](https://getkirby.com/docs/reference/objects/cms/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 👍

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 1, 2021, 4:20pm UTC](https://forum.getkirby.com/t/can-i-change-media-cache-write-function-to-use-do-spaces-or-aws-s3/23829/2 "2021-11-01T16:20:53Z")

</div>

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

> **[File versions](https://getkirby.com/docs/reference/plugins/components/file-version)**

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

> **[File URLs](https://getkirby.com/docs/reference/plugins/components/file-urls)**

Other useful resources:

> **[Virtual files](https://getkirby.com/docs/cookbook/extensions/virtual-files)**
>
> Use files from outside of Kirby’s file system as first class citizens

> **[Kirby loves CDN](https://getkirby.com/docs/cookbook/extensions/kirby-loves-cdn)**
>
> Set up custom components to deliver assets and files through a CDN.

---

<div class="post-metadata">

**Author:** ![simon-hayden](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/simon-hayden/32/4362_2.png) [@simon-hayden](https://forum.getkirby.com/u/simon-hayden)\
**Post date:** [November 1, 2021, 4:33pm UTC](https://forum.getkirby.com/t/can-i-change-media-cache-write-function-to-use-do-spaces-or-aws-s3/23829/3 "2021-11-01T16:33:05Z")

</div>

Thanks, @texnixe. I saw your [Kirby loves CDN](https://getkirby.com/docs/cookbook/extensions/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?

```auto
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.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 1, 2021, 5:01pm UTC](https://forum.getkirby.com/t/can-i-change-media-cache-write-function-to-use-do-spaces-or-aws-s3/23829/4 "2021-11-01T17:01:40Z")

</div>

> [@simon-hayden](#):
>
> `'media::publish'`

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.
