sunkan / php-mogilefs
php 的 MogileFs 客户端
3.1.0
2023-09-27 08:41 UTC
Requires
- php: ^8.0
- ext-json: *
- psr/http-message: ~1.0
Requires (Dev)
- friends-of-phpspec/phpspec-code-coverage: ^6.2@dev
- fzaninotto/faker: ^1.0
- mockery/mockery: ^1.0
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: 9.*
README
安装
安装此库的首选方法是使用 Composer,在项目根目录下运行以下命令:
$ composer require sunkan/php-mogilefs
文档
实例化
<?php $connection = new MogileFs\Connection([ '127.0.0.1:7001' ]); //or $connection = new MogileFs\Connection([ [ 'host' => '127.0.0.1', 'port' => 7001 ] ]);
域名客户端
创建域名
$domainClient = new MogileFs\Client\DomainClient($connection); try { $domain = $domainClient->create('example.com'); $domain->getDomain(); // domain name $domain->getClasses(); // array of classes } catch(MogileFs\Exception $e) { $e->getMessage() === 'domain_exists'; }
删除域名
$domainClient = new MogileFs\Client\DomainClient($connection); try { $response = $domainClient->delete('example.com'); $response->isSuccess(); } catch(MogileFs\Exception $e) { $e->getMessage() === 'domain_not_found'; }
列出域名
$domainClient = new MogileFs\Client\DomainClient($connection); $collection = $domainClient->all(); foreach ($collection as $domain) { $domain->getDomain(); // domain name $domain->getClasses(); // array of classes }
类客户端
实例化
$classClient = new MogileFs\Client\ClassClient($connection, 'example.com'); //or $classClient = new MogileFs\Client\ClassClient($connection); $classClient->setDomain('example.com');
创建类
$classClient = new MogileFs\Client\ClassClient($connection, 'example.com'); try { $replicationCount = 2; $class = $classClient->create('assets', $replicationCount); $class->getName(); // class name $class->getCount(); // replication count $class->getPolicy(); // replication policy $class->getHash(); // hash policy $class->getDomain(); // domain name } catch(MogileFs\Exception $e) { $e->getMessage() === 'class_exists'; }
更新类
$classClient = new MogileFs\Client\ClassClient($connection, 'example.com'); try { $newReplicationCount = 4; $class = $classClient->update('assets', $replicationCount); } catch(MogileFs\Exception $e) { $e->getMessage() === 'class_not_found'; }
删除类
$classClient = new MogileFs\Client\ClassClient($connection, 'example.com'); try { $response = $classClient->delete('assets'); $response->isSuccess(); } catch(MogileFs\Exception $e) { $e->getMessage() === 'class_not_found'; }
文件客户端
实例化
$fileClient = new MogileFs\Client\FileClient($connection, 'example.com'); //or $fileClient = new MogileFs\Client\FileClient($connection); $fileClient->setDomain('example.com');
获取路径
$fileClient = new MogileFs\Client\FileClient($connection, 'example.com'); try { $path = $fileClient->get('test/key'); $path->getPath(); // first path $path->getPaths(); // array of paths $path->getCount(); // number of paths } catch(MogileFs\Exception $e) { $e->getMessage() === 'unknown_key'; }
获取文件信息
$fileClient = new MogileFs\Client\FileClient($connection, 'example.com'); try { $file = $fileClient->info('test/key'); $file->getFid(); // file id $file->getKey(); // file key - test/key $file->getSize(); // file size $file->getFileCount(); // replication count $file->getDomain(); // file domain $file->getClass(); // file class } catch(MogileFs\Exception $e) { $e->getMessage() === 'unknown_key'; }
删除文件
$fileClient = new MogileFs\Client\FileClient($connection, 'example.com'); $response = $fileClient->delete('test/key'); $response->isSuccess();
重命名文件
$fileClient = new MogileFs\Client\FileClient($connection, 'example.com'); $response = $fileClient->rename('test/key', 'test/key2'); $response->isSuccess();
列出文件
$fileClient = new MogileFs\Client\FileClient($connection, 'example.com'); $collection = $fileClient->listKeys($prefix, $suffix, $limit); $collection; // contains file keys nothing else $collection = $fileClient->listFids($fromFid, $toFid); $collection; // contains File objects with information about all files in range
上传文件
上传函数接受实现了 MogileFs\File\FileInterface
的任何对象
文件类型
Blob
$class = "blob-class"; $file = new MogileFs\File\BlobFile('raw data', $class)
本地文件
$class = "local-file-class"; $file = new MogileFs\File\LocalFile('/path/to/file', $class)
Psr-7 文件上传
$class = "psr7-file-class"; $uploadFile = $request->getUploadedFiles()['file']; $file = new MogileFs\File\Psr7File($uploadFile, $class) //or $stream;// instance of Psr\Http\Message\StreamInterface $file = new MogileFs\File\Psr7File($stream, $class)
资源
$class = "resource-file-class"; $resource = fopen('file', 'r'); // should work with any stream context $file = new MogileFs\File\ResourceFile($resource, $class)
工厂
$factory = new MogileFs\File\Factory(); $file = $factory($fileContent, $class);
上传
$fileClient = new MogileFs\Client\FileClient($connection, 'example.com'); $factory = new MogileFs\File\Factory(); $key = 'test/key'; $file = $factory($fileContent, $class); $response = $fileClient->upload($key, $file); if ($response->isSuccess()) { $path = $fileClient->get($key); }