Publish AssetManager files to CDN

Is there any good solution or existing extension for an Yii AssetManager which stores the files on a CDN storage like S3?

Motivation behind:

Our main problem is, that we have multiple webservers scaled horizontally, managed by K8s. Therefore Assets should be uploaded once and served as CDN url afterwards. It would be nice to have a solution on the build process of the application, but i think this won’t work due to the fact we don’t know all the assets which are used. Therefore i would develop an AssetManager which uploads the files to a CDN, stores this information in the cache (file x is uploaded) and just serve the URL in future.

I could not find any good extension or any good class which extends the AssetManager. The AssetManager does not have an interface which lets me know how to create my own AssetManager (https://www.yiiframework.com/doc/api/2.0/yii-web-assetmanager).

Any help or tipp regarding my AssetManager problem would be nice. If i can manage to get my task done, i will publish the library as oss of course.

Yes. https://packagist.org/packages/mikk150/yii2-asset-manager-flysystem

I’ve posted an article in Russian with some examples. May be read via Google translate: https://rmcreative.ru/blog/post/yii-2-za-balansirovschikom-nagruzki

Thanks, that gave me some very good idea of how to build such a system.

There are still some issue, for instance there is a check for whether the base path is writable or not (https://github.com/yiisoft/yii2/blob/master/framework/web/AssetManager.php#L443) but this should be either done in the init() or configurable, because its not needed when working with a s3 storage (for instance).

Would you accept a PR which moves this part into the initializer? As its checking the same path over and over again for every publish action, might even increase performance due to less disc read.

Yes, I think that would be a good change.

Hi @samdark mikk150/yii2-asset-manager-flysystem uploads the assets to s3 right?

Not sure what i’m missing here, It reads from s3 correctly, but i had to upload my assets manually to s3. Here is my settings

'assetManager' =>  [
            'class'    => \white43\CloudAssetManager\CloudAssetManager::class,
            'basePath' => 'assets',
            'cache'    => 'cache', // Name of your cache component
            'baseUrl'  => 'https://storage.blabla.com/assets',
            'filesystem' => [
                'class'  => creocoder\flysystem\AwsS3Filesystem::class,
                'key'    => 'xxxxxxxxxxxxxxxxxxx',
                'secret' => 'xxxxxxxxxxxxxxxxxxx',
                'region' => 'ap-southeast-1',
                'bucket' => 'storage.blabla.com',
            ],
        ]

Do you get any error when executing it?

nope, my site works fine. no errors in logs either. only thing is i have to upload the assets manually.