PHP code example of bilberrry / yii2-digitalocean-spaces

1. Go to this page and download the library: Download bilberrry/yii2-digitalocean-spaces library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bilberrry / yii2-digitalocean-spaces example snippets


    'components' => [
        // ...
        'storage' => [
            'class' => 'bilberrry\spaces\Service',
            'credentials' => [
                'key' => 'my-key',
                'secret' => 'my-secret',
            ],
            'region' => 'nyc3', // currently available: nyc3, ams3, sgp1, sfo2
            'defaultSpace' => 'my-space',
            'defaultAcl' => 'public-read',
        ],
        // ...
    ],
    

/** @var \bilberrry\spaces\Service $storage */
$storage = Yii::$app->get('storage');

/** @var \Aws\ResultInterface $result */
$result = $storage->commands()->get('filename.ext')->saveAs('/path/to/local/file.ext')->execute();

$result = $storage->commands()->put('filename.ext', 'body')->withContentType('text/plain')->execute();

$result = $storage->commands()->delete('filename.ext')->execute();

$result = $storage->commands()->upload('filename.ext', '/path/to/local/file.ext')->withAcl('private')->execute();

$result = $storage->commands()->restore('filename.ext', $days = 7)->execute();

$result = $storage->commands()->list('path/')->execute();

/** @var bool $exist */
$exist = $storage->commands()->exist('filename.ext')->execute();

/** @var string $url */
$url = $storage->commands()->getUrl('filename.ext')->execute();

/** @var string $signedUrl */
$signedUrl = $storage->commands()->getPresignedUrl('filename.ext', '+2 days')->execute();

/** @var \bilberrry\spaces\Service $storage */
$storage = Yii::$app->get('storage');

/** @var \Aws\ResultInterface $result */
$result = $storage->get('filename.ext');

$result = $storage->put('filename.ext', 'body');

$result = $storage->delete('filename.ext');

$result = $storage->upload('filename.ext', '/path/to/local/file.ext');

$result = $storage->restore('filename.ext', $days = 7);

$result = $storage->list('path/');

/** @var bool $exist */
$exist = $storage->exist('filename.ext');

/** @var string $url */
$url = $storage->getUrl('filename.ext');

/** @var string $signedUrl */
$signedUrl = $storage->getPresignedUrl('filename.ext', '+2 days');

/** @var \bilberrry\spaces\Service $storage */
$storage = Yii::$app->get('storage');

/** @var \GuzzleHttp\Promise\PromiseInterface $promise */
$promise = $storage->commands()->get('filename.ext')->async()->execute();

$promise = $storage->commands()->put('filename.ext', 'body')->async()->execute();

$promise = $storage->commands()->delete('filename.ext')->async()->execute();

$promise = $storage->commands()->upload('filename.ext', 'source')->async()->execute();

$promise = $storage->commands()->list('path/')->async()->execute();

/** @var \bilberrry\spaces\interfaces\Service $storage */
$storage = Yii::$app->get('storage');

/** @var \frostealth\yii2\aws\s3\commands\GetCommand $command */
$command = $storage->create(GetCommand::class);
$command->inSpace('my-another-space')->byFilename('filename.ext')->saveAs('/path/to/local/file.ext');

/** @var \Aws\ResultInterface $result */
$result = $storage->execute($command);

// or async
/** @var \GuzzleHttp\Promise\PromiseInterface $promise */
$promise = $storage->execute($command->async());



namespace app\components\s3\commands;

use bilberrry\spaces\base\commands\traits\Options;
use bilberrry\spaces\interfaces\commands\Command;
use bilberrry\spaces\interfaces\commands\HasSpace;

class MyCommand implements Command, HasSpace
{
    use Options;

    protected $space;

    protected $something;

    public function getSpace()
    {
        return $this->space;
    }

    public function inSpace(string $space)
    {
        $this->space = $space;

        return $this;
    }

    public function getSomething()
    {
        return $this->something;
    }

    public function withSomething(string $something)
    {
        $this->something = $something;

        return $this;
    }
}



namespace app\components\s3\handlers;

use app\components\s3\commands\MyCommand;
use frostealth\yii2\aws\s3\base\handlers\Handler;

class MyCommandHandler extends Handler
{
    public function handle(MyCommand $command)
    {
        return $this->s3Client->someAction(
            $command->getSpace(),
            $command->getSomething(),
            $command->getOptions()
        );
    }
}

/** @var \bilberrry\spaces\interfaces\Service */
$storage = Yii::$app->get('storage');

/** @var \app\components\s3\commands\MyCommand $command */
$command = $storage->create(MyCommand::class);
$command->withSomething('some value')->withOption('OptionName', 'value');

/** @var \Aws\ResultInterface $result */
$result = $storage->execute($command);



namespace app\components\s3\commands;

use bilberrry\spaces\interfaces\commands\HasSpace;
use bilberrry\spaces\interfaces\commands\PlainCommand;

class MyPlainCommand implements PlainCommand, HasSpace
{
    protected $args = [];

    public function getSpace()
    {
        return $this->args['Bucket'] ?? '';
    }

    public function inSpace(string $space)
    {
        $this->args['Bucket'] = $space;

        return $this;
    }

    public function getSomething()
    {
        return $this->args['something'] ?? '';
    }

    public function withSomething($something)
    {
        $this->args['something'] = $something;

        return $this;
    }

    public function getName(): string
    {
        return 'SpaceCommandName';
    }

    public function toArgs(): array
    {
        return $this->args;
    }
}