filestack / filestack-php
PHP的Filestack库
2.0.5-beta
2024-09-18 18:04 UTC
Requires
- php: >=8.3
- guzzlehttp/guzzle: ~7.8
Requires (Dev)
- mockery/mockery: ^1.0
- php-coveralls/php-coveralls: ^2.2
- phpmd/phpmd: @stable
- phpunit/phpunit: ^9.0
- theseer/phpdox: ^0.7.0
This package is auto-updated.
Last update: 2024-09-18 18:21:40 UTC
README
Filestack PHP
这是Filestack官方的PHP SDK - API和内容管理系统,可轻松将强大的文件上传和转换功能添加到任何Web或移动应用中。要求
- PHP 8.3+
资源
安装
使用composer安装filestack
,可以运行
$ composer require --prefer-dist filestack/filestack-php
用法
Filestack库提供对三个有用类的访问
FilestackClient
- 用于简单上传文件(创建Filelink对象)Filelink
- 用于文件处理(下载、转换等)FileSecurity
- 用于将策略和签名值应用到API调用中
上传文件
首先,您需要创建FilestackClient实例
use Filestack\FilestackClient; $client = new FilestackClient('YOUR_API_KEY');
调用upload()函数
$filelink = $client->upload('/path/to/file');
存储
默认情况下,使用Amazon S3存储您的文件。如果您希望使用其他存储服务,可以在调用upload()和store()时传递额外的参数'location'
$client = new FilestackClient('YOUR_API_KEY'); $extras = [ 'Location' => 'dropbox', 'Filename' => 'somefilename.jpg', ]; $filepath = '/path/to/file'; $filelink = $client->upload($filepath); # get metadata of file $metadata = $client->getMetaData($filelink->handle, $fields); # get content of a file $content = $client->getContent($filelink->handle); # download a file $destination = '/path/to/file'; $result = $client->download($filelink->handle, $destination); # overwrite a file $filelink2 = $client->overwrite('/path/to/file', $filelink->handle);
处理文件
Filelink对象可以通过两种方式创建
- 使用FilestackClient上传文件
- 使用文件句柄和api_key初始化Filelink
上面展示了第一种方法,第二种方法也很简单,将创建代表已上传文件的对象。
use Filestack\filelink; $filelink = new Filelink('some-file-handle', 'YOUR_API_KEY'); # transforming an image $transformed_filelink = $filelink ->circle() ->blur(['amount' => '20']) ->save(); # get metadata $metadata = $filelink->getMetaData(); # get content of a file $content = $filelink->getContent(); $filepath = '/path/to/file'; # download a file $filelink->download($filepath); # overwrite remote file with local file $filelink->overwrite($filepath); # delete remote file $filelink->delete();
标记文件和检测工作安全内容
use Filestack\FilestackClient; use Filestack\FilestackSecurity; $security = new FilestackSecurity('YOUR_SECURITY_SECRET'); $client = new FilestackClient('YOUR_API_KEY', $security); $file_handle = 'some-file-handle'; # get tags with client $result_json = $client->getTags($file_handle); # get tags with filelink $filelink = new Filelink($file_handle, 'YOUR_API_KEY', $security); $json_result = $filelink->getTags(); # get safe for work flag with client $result_json = $client->getSafeForWork($file_handle); # get safe for work flag with filelink $json_result = $filelink->getSafeForWork();
有关更多示例,请参阅此项目中的examples/
文件夹:examples/
智能摄取
智能摄取功能允许用户以不确定大小的块上传文件。这创建了一个更稳定的上传流程,确保正在上传的文件最终将成功完成,无论网络延迟或超时错误。
但是,对于大文件,上传过程可能比正常上传流程慢,因为错误会使用指数退避重试策略重试。
最后,此功能必须启用api_key。要启用此功能,请联系Filestack,邮箱:support@filestack.com。
$client = new FilestackClient('YOUR_API_KEY');
$filelink = $client->upload('/path/to/file', ['intelligent' => true]);
版本控制
Filestack PHP SDK遵循语义版本控制。
代码规范
- PSR-2编码规范(http://www.php-fig.org/psr/psr-2/)
- PSR-4自动加载规范(http://www.php-fig.org/psr/psr-4/)
- phpDoc文档注释规范(https://www.phpdoc.org/docs/latest/getting-started/your-first-set-of-documentation.html)
测试
- 要运行测试,从项目根目录运行
vendor/bin/phpunit
- 要生成覆盖率报告,运行以下命令(将在coverage/目录下生成HTML文件)
vendor/bin/phpunit --coverage-xml=coverage
- 要运行PHPMD进行CodeClimate检查
vendor/bin/phpmd filestack xml phpmd-rules.xml > logs/phpmd-report-filestack.xml
vendor/bin/phpmd tests xml phpmd-rules.xml > logs/phpmd-report-tests.xml
生成文档
要获取项目度量,使用phar文件https://github.com/sebastianbergmann/phploc
./phploc.phar --log-xml=phploc.xml .
要生成文档,请使用来自 https://github.com/theseer/phpdox 的 phar 文件。
./phpdox.phar
问题
如果您遇到问题,请创建一个 Github Issue。
贡献
有关详细信息,请参阅 CONTRIBUTING.md。
致谢
感谢所有 贡献者。