softavis/flysystem-cloudflare

Flysystem Adapter for Cloudflare Images

0.3.1 2024-05-14 08:39 UTC

This package is auto-updated.

Last update: 2024-11-12 14:17:19 UTC


README

Cloudflare images filesystem for Flysystem.

codecov Packagist Downloads GitHub License GitHub commit activity

This library implement cloudflare images API to work with flysystem abstraction.

Cloudflare Images support only Image files, obviously

Installation

composer require softavis/flysystem-cloudflare

Usage

<?php

declare(strict_types=1);

use League\Flysystem\Config;
use Softavis\Flysystem\Cloudflare\Client;
use Softavis\Flysystem\Cloudflare\CloudflareAdapter;
use Symfony\Component\HttpClient\HttpClient;

require './vendor/autoload.php';

const CLOUDFLARE_API_KEY = 'your-cloudflare-access-token';
const CLOUDFLARE_ACCOUNT_ID = 'your-cloudflare-account-id';
const CLOUDFLARE_ACCOUNT_HASH = 'your-cloudflare-account-hash';
const CLOUDFLARE_VARIANT_NAME = 'your-cloudflare-images-variant';

const CLOUDFLARE_URL = 'https://api.cloudflare.com/client/v4/accounts/%s/images/';

$client = new Client(HttpClient::createForBaseUri(sprintf(CLOUDFLARE_URL, CLOUDFLARE_ACCOUNT_ID), [
    'auth_bearer' => CLOUDFLARE_API_KEY,
]));

$adapter = new CloudflareAdapter($client);

$flysystem = new League\Flysystem\Filesystem($adapter, [
    'accountHash' => CLOUDFLARE_ACCOUNT_HASH,
    'variantName' => CLOUDFLARE_VARIANT_NAME
]);

// see http://flysystem.thephpleague.com/api/ for full list of available functionality

Usage with Symfony

Add env's

CLOUDFLARE_API_KEY=
CLOUDFLARE_ACCOUNT_ID=
CLOUDFLARE_ACCOUNT_HASH=
CLOUDFLARE_VARIANT_NAME=

First, add cloudflare scoped client (edit config/packages/framework.yaml) and add:

scoped_clients:
    cloudflare.client:
        base_uri: 'https://api.cloudflare.com/client/v4/accounts/%env(CLOUDFLARE_ACCOUNT_ID)%/images/'
        auth_bearer: '%env(CLOUDFLARE_API_KEY)%'

Next, add cloudflare flysystem services (edit config/services.yaml):

cloudflare_adapter:
    class: 'Softavis\Flysystem\Cloudflare\CloudflareAdapter'
    arguments:
        - '@cloudflare_client'
        - '%env(CLOUDFLARE_ACCOUNT_HASH)%'
        - '%env(CLOUDFLARE_VARIANT_NAME)%'

cloudflare_client:
    class: 'Softavis\Flysystem\Cloudflare\Client'
    arguments:
        - '@cloudflare.client' # This argument is our scoped client
        - '%env(CLOUDFLARE_ACCOUNT_HASH)%'
        - '%env(CLOUDFLARE_VARIANT_NAME)%'

Last, add cloudflare_adapter to flysystem configuration (edit config/packages/flysystem.yaml)

flysystem:
    storages:
        default.storage:
            adapter: 'cloudflare_adapter'

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Contributions are welcome and will be fully credited. We accept contributions via Pull Requests on Github.

Pull Requests

  • PSR-2 Coding Standard - The easiest way to apply the conventions is to install PHP Code Sniffer.
  • Document any change in behaviour - Make sure the README.md and any other relevant documentation are kept up-to-date.
  • Create feature branches - Don't ask us to pull from your master branch.
  • One pull request per feature - If you want to do more than one thing, send multiple pull requests.

License

The MIT License (MIT). Please see License File for more information.