FlySystem Adapter for Bunny.net

A FlySystem adapter for BunnyCDN's storage

FlySystem Adapter for Bunny.net

Build Status - Flysystem v1 Build Status - Flysystem v1 Build Status - Flysystem v1
Codecov Packagist Version Minimum PHP Version: 7.2 Licence: MIT Downloads

I'm a big fan of bunny.net and their CDN / Storage offering, but found their PHP Storage API a bit lacking.

bunny.net is a simple but powerful content delivery platform, offering lightning fast performance for a fraction of the cost.

Using the FlySystem abstraction, I've created the flysystem-bunnycdn package to allow you to not have to learn a new API in order to interact with the Bunny.net storage API.

GitHub - PlatformCommunity/flysystem-bunnycdn: A FlySystem adapter for BunnyCDN’s storage
A FlySystem adapter for BunnyCDN’s storage. Contribute to PlatformCommunity/flysystem-bunnycdn development by creating an account on GitHub.

Installation

To install flysystem-bunnycdn, require the package with no version constraint. It should match the flysystem-bunnycdn version with your version of FlySystem (v1, v2, v3 etc).

composer require platformcommunity/flysystem-bunnycdn "*"

Usage

use League\Flysystem\Filesystem;
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNAdapter;
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNClient;
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNRegion;

$adapter = new BunnyCDNAdapter(
    new BunnyCDNClient(
        'storage-zone', 
        'api-key', 
        BunnyCDNRegion::FALKENSTEIN
    )
);

$filesystem = new Filesystem($adapter);

Usage with Pull Zones

To have BunnyCDN adapter publish to a public CDN location, you have to a "Pull Zone" connected to your BunnyCDN Storage Zone. Add the full URL prefix of your Pull Zone (including http:///https://) to the BunnyCDNAdapter parameter like shown below.

use League\Flysystem\Filesystem;
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNAdapter;
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNClient;
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNRegion;

$adapter = new BunnyCDNAdapter(
    new BunnyCDNClient(
        'storage-zone',
        'api-key',
        BunnyCDNRegion::FALKENSTEIN
    ),
    'https://testing.b-cdn.net/' # Pull Zone URL
);
$filesystem = new Filesystem($adapter);

Note: You can also use your own domain name if it's configured in the pull zone.

Once you add your pull zone, you can use the ->getUrl($path), or in Laravel, the ->url($path) command to get the fully qualified public URL of your BunnyCDN assets.

Usage in Laravel 9

To add BunnyCDN adapter as a custom storage adapter in Laravel 9, install using the v3 composer installer.

composer require platformcommunity/flysystem-bunnycdn "^3.0"

Next, install the adapter to your AppServiceProvider to give Laravel's FileSystem knowledge of the BunnyCDN adapter.

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Storage::extend('bunnycdn', function ($app, $config) {
            $adapter = new BunnyCDNAdapter(
                new BunnyCDNClient(
                    $config['storage_zone'],
                    $config['api_key'],
                    $config['region']
                ),
                'http://testing.b-cdn.net' # Optional
            );

            return new FilesystemAdapter(
                new Filesystem($adapter, $config),
                $adapter,
                $config
            );
        });
    }

Finally, add the bunnycdn driver into your config/filesystems.php configuration file.

        ... 
        
        'bunnycdn' => [
            'driver' => 'bunnycdn',
            'storage_zone' => env('BUNNYCDN_STORAGE_ZONE'),
            'api_key' => env('BUNNYCDN_APY_KEY'),
            'region' => env('BUNNYCDN_REGION')
        ],
        
        ...

After populating your BUNNYCDN_STORAGE_ZONE, BUNNYCDN_APY_KEY BUNNYCDN_REGION variables in your .env file, you can then use the Storage facade with BunnyCDN.

Storage::disk('bunnycdn')->put('index.html', '<html>Hello World</html>');

return response(Storage::disk('bunnycdn')->get('index.html'));

Note: You may have to run php artisan config:clear in order for your configuration to be refreshed if your app is running with a config cache driver / production mode.