garantpark / selectel
Selectel云存储API客户端
Requires
- php: >=5.4.0
- guzzlehttp/guzzle: ^6.2
This package is not auto-updated.
Last update: 2024-09-14 19:31:05 UTC
README
安装
通过Composer
$ composer require garantpark/selectel
基本用法
<?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
可用方法
存储信息
$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');
如果容器不存在,将抛出\GarantPark\Selectel\Exceptions\ContainerNotFoundException
异常。
删除容器
$client->deleteContainer('container-name');
如果容器不存在,将抛出\GarantPark\Selectel\Exceptions\ContainerNotFoundException
异常。
此外,如果容器不为空,将抛出\GarantPark\Selectel\Exceptions\ContainerNotEmptyException
异常。
获取容器文件
$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);
如果容器不存在,将抛出\GarantPark\Selectel\Exceptions\ContainerNotFoundException
异常。
上传文件
$localFile = realpath('../../images/logo-big.png'); $remotePath = '/images/logos/logo-big.png'; $file = $client->uploadFile('container-name', $localFile, $remotePath);
如果本地文件不可读,将抛出\GarantPark\Selectel\Exceptions\LocalFileNotAvailableException
异常。
如果上传失败,将抛出GarantPark\Selectel\Exceptions\FileUploadFailedException
异常。
存档上传和解包
使用与文件上传相同的方法,但添加第三个参数$params
,包含extract-archive
键。
$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'.
如果上传失败,将抛出GarantPark\Selectel\Exceptions\FileUploadFailedException
异常。
如果存档解包失败,将抛出GarantPark\Selectel\Exceptions\ArchiveExtractFailedException
异常。
支持的存档类型
- 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);
这将向文件添加X-Object-Meta-Created-By
头,值为备份管理器
。
如果容器不存在,将抛出GarantPark\Selectel\Exceptions\ContainerNotFoundException
异常。
复制文件
$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);
如果源或目标路径不存在,将抛出GarantPark\Selectel\Exceptions\ObjectNotFoundException
异常。
删除文件
$client->deleteFile('container-name', '/path/to/fille.png');
如果文件不存在,将抛出GarantPark\Selectel\Exceptions\FileNotFoundException
异常。
创建文件或目录的符号链接
$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);
可用的符号链接类型
symlink
:基本链接onetime-symlink
:一次性链接(在第一次访问后将删除)symlink+secure
:带密码保护的链接onetime-symlink+secure
:带密码保护的一次性链接
如果您使用受密码保护的链接,用户必须使用_sslk
查询参数来获取此文件的访问权限:http://XXXX.selcdn.ru/container-name/public/images/structure.png?sslk=mySecurePassword
为私有文件生成下载链接
首先,您需要将密钥设置为账户或容器(一次性)。
$containerName = 'container-name'; // container-specific secret key $client->setObjectSecretKey($containerName, 'myContainerSecretKey'); // or account-wide secret key $client->setObjectSecretKey('/', 'myAccountSecretKey');
如果容器不存在,将抛出GarantPark\Selectel\Exceptions\ObjectNotFoundException
异常。
完成此操作后,您将能够生成签名链接。
$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);
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。