Skip to content

Selectel Cloud Storage API Client

License

Notifications You must be signed in to change notification settings

Kron0S/selectel

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Selectel Cloud Storage API

Latest Stable Version License

Selectel's Cloud Storage API Client.

Install

Via Composer

$ composer require garantpark/selectel

Basic Usage

<?php
use GarantPark\Selectel\Client;
$client = new Client($username, $password);
$client->createContainer('new-container', 'public');

$localPath = realpath('./image.png');
$remotePath = '/images/image.png';
$client->uploadFile('new-container', $localPath, $remotePath);
// File image.png is now available at http://XXX.selcdn.ru/new-container/images/image.png
// XXX in URL is your Storage ID

Available Methods

$storageInfo = $client->getStorageInfo();
$containers = $client->getContainers();
$containerType = 'public'; // or 'private'
$metaData = []; // Any meta data (will be stored as "X-Container-Meta-{KEY}: {Value}" header)
$result = $client->createContainer('container-name', $containerType, $metaData);
$containerInfo = $client->getContainerInfo('container-name');

If container does not exists, \GarantPark\Selectel\Exceptions\ContainerNotFoundException will be thrown.

$client->deleteContainer('container-name');

If container does not exists, \GarantPark\Selectel\Exceptions\ContainerNotFoundException will be thrown.

Also, if container is not empty, \GarantPark\Selectel\Exceptions\ContainerNotEmptyException will be thrown.

$params = [
    'path' => '/images/', // get files from /images/ direcory only
];
// More $params values can be found in Selectel's API docs (see link above).
$files = $client->getContainerFiles('container-name', $params);

If container does not exists, \GarantPark\Selectel\Exceptions\ContainerNotFoundException will be thrown.

$localFile = realpath('../../images/logo-big.png');
$remotePath = '/images/logos/logo-big.png';
$file = $client->uploadFile('container-name', $localFile, $remotePath);

If local file is not readable, \GarantPark\Selectel\Exceptions\LocalFileNotAvailableException will be thrown.

If upload fails, GarantPark\Selectel\Exceptions\FileUploadFailedException will be thrown.

Use same method as for file upload but add third argument $params with extract-archive key.

$localFile = realpath('../../backups/database-backup-latest.tar.gz');
$remotePath = '/backups/database/' . date('Y-m-d') . '.tar.gz';
$params = [
    'extract-archive' => 'tar.gz',
];
$archive = $client->uploadFile('container-name', $localFile, $remotePath, $params);
// $archive will contain additional field 'extract.filesCount'.

If upload fails, GarantPark\Selectel\Exceptions\FileUploadFailedException will be thrown.

If archive extraction fails, GarantPark\Selectel\Exceptions\ArchiveExtractFailedException will be thrown.

Supported archive types:

  • tar
  • tar.gz
  • tar.bz2
$remotePath = '/backups/database/' . date('Y-m-d') . 'tar.gz'; // cloud path
$meta = [
    'Created-By' => 'Backup Manager',
];
$client->setFileMetaData('container-name', $remotePath, $meta);

This will result in adding X-Object-Meta-Created-By header to file with value Backup Manager.

If container does not exists, GarantPark\Selectel\Exceptions\ContainerNotFoundException will be thrown.

$srcContainer = 'images-old'; // source container name
$srcPath = '/images/logos/logo-big.png'; // source path (in container)
$dstContainer = 'images-new'; // destination container name
$dstPath = '/images/logos/logo-big-copied.png'; // destination path (in container)
// optional metadata
$meta = [
    'Copied-At' => date('Y-m-d H:i:s'), // add "X-Object-Meta-Copied-At: date" header to copied file
];
$client->copyFile($srcContainer, $srcPath, $dstContainer, $dstPath, $meta);

If source or destination path does not exists, GarantPark\Selectel\Exceptions\ObjectNotFoundException will be thrown.

$client->deleteFile('container-name', '/path/to/fille.png');

If file does not exists, GarantPark\Selectel\Exceptions\FileNotFoundException will be thrown.

$linkPath = '/public/images/structure.png'; // link path (where to store link)
$linkSource = '/private-images/structure.png'; // original file
$params = [
    'type' => 'symlink', // link type, see information below, required
    'deleteAt' => time() + (60 * 60), // link will be deleted in one hour, optional
    'password' => sha1('mySecurePassword' . urlencode($linkSource)), // password, optional
];
$client->createSymlink('container-name', $linkPath, $linkSource, $params);

Available symlink types:

  • symlink: basic link,
  • onetime-symlink: onetime link (will be deleted after first visit),
  • symlink+secure: link with password protection
  • onetime-symlink+secure: onetime link with password protection

If you're using password protected link, user must use _sslk query param to get access to this file: http://XXXX.selcdn.ru/container-name/public/images/structure.png?sslk=mySecurePassword

First of all, you need to set up secret key to an account or a container (one time).

$containerName = 'container-name';
// container-specific secret key
$client->setObjectSecretKey($containerName, 'myContainerSecretKey');
// or account-wide secret key
$client->setObjectSecretKey('/', 'myAccountSecretKey');

If container does not exists, GarantPark\Selectel\Exceptions\ObjectNotFoundException will be thrown.

After doing so, you'll be able to generate signed links.

$remotePath = '/container-name/private-files/account.txt';
$expires = time() + (60 * 60); // expires in 1 hour
$secretKey = 'myAccountSecretKey'; // account or container secret key
$signedUrl = $client->generateSignedLink($remotePath, $expires, $secretKey);

License

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

About

Selectel Cloud Storage API Client

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%