ejunker / filestack-php
PHP 的 Filestack 库
1.2.0
2021-09-28 15:23 UTC
Requires
- php: >=7.3
- guzzlehttp/guzzle: ~6.5 || ~7.3
Requires (Dev)
- mockery/mockery: dev-master
- php-coveralls/php-coveralls: ^2.2
- phpmd/phpmd: @stable
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-28 21:31:19 UTC
README
Filestack PHP
这是 Filestack 的官方 PHP SDK,Filestack 是一个 API 和内容管理系统,它使得向任何 Web 或移动应用程序添加强大的文件上传和转换功能变得简单。要求
- PHP 7.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/ 文件夹。
智能摄取
智能摄取功能允许用户以不精确大小的块上传文件。这创建了一个更稳定的上传流程,确保上传的文件最终会成功完成,无论网络延迟或超时错误。
然而,对于大文件,上传过程可能比正常上传流程慢,因为使用指数退避重试策略重试了错误。
最后,此功能必须为使用的 apikey 启用。要启用此功能,请联系 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
生成文档
要获取项目度量,使用 https://github.com/sebastianbergmann/phploc 的 phar 文件
./phploc.phar --log-xml=phploc.xml .
要生成文档,使用 https://github.com/theseer/phpdox 的 phar 文件
./phpdox.phar
问题
如果您有问题,请创建 Github Issue。
贡献
请参阅CONTRIBUTING.md以获取详细信息。
致谢
感谢所有贡献者。