jeansouzak/duf
Composer 安装命令:
composer require jeansouzak/duf
包简介
DUF (Download and Upload Files) - Fetch URL file, download and upload to a service
README 文档
README
Duf is a PHP package to download files from web/local and upload to other servers (Google Cloud Storage).
- First package version: Download resources from web and local to upload to Google Cloud Storage ☑
- Second package version: Create Duffer adapter to upload to Amazon S3 ☐
Installing Duf
The recommended way to install Duf is through Composer.
composer require jeansouzak/duf
How it works:
use JeanSouzaK\Duf\Duff; use JeanSouzaK\Duf\Prepare\WebResource; $duf = Duff::getInstance(Duff::GOOGLE_CLOUD_STORAGE, [ 'project_id' => 'storage-test-227305', 'key_path' => '/home/env/storage-test-227305-8472347a39489.json' ]); // - Chaining example - $uploadResults = $duf->prepare([ new WebResource('teste.png', 'https://dummyimage.com/600x400/000/fff') ])->download()->addBucket('meubucket666')->upload(); // - Step-by-step example - //Prepare object name and url $duf->prepare([ new WebResource('object_one.png', 'https://dummyimage.com/600x400/000/fff'), new WebResource('object_two.png', 'https://dummyimage.com/300x200/000/fff'), new WebResource('object_three.png', 'https://dummyimage.com/100x100/000/fff') ]); //Make download prepared files $duf->download(); //Add google cloud storage bucket name $duf->addBucket('meubucket666'); //Upload downloaded files to bucket and get results $uploadResults = $duf->upload(); // Iterate results and get result path echo '<pre>'; foreach ($uploadResults as $uploadResult) { print_r($uploadResult); echo (string)$uploadResult.'<br/>'; }
Duf will help you if you desire upload a local file as well:
use JeanSouzaK\Duf\Duff; use JeanSouzaK\Duf\Prepare\LocalResource; use JeanSouzaK\Duf\Prepare\WebResource; //Call factory to get google cloud storage duffer $duf = Duff::getInstance(Duff::GOOGLE_CLOUD_STORAGE, [ 'project_id' => 'storage-test-227305', 'key_path' => '/home/env/storage-test-227305-8472347a39489.json' ]); //Uploading from local file target $duf->prepare([ new LocalResource('imagem', '/home/test/images/test.jpg') ]); //You can mix local and remote target file $duf->prepare([ new LocalResource('imagem', '/home/test/images/test.jpg'), new WebResource('teste.png', 'https://dummyimage.com/600x400/000/fff') ]); //Make download prepared files $duf->download(); //Add google cloud storage bucket name $duf->addBucket('meubucket666'); //Upload downloaded files to bucket and get results $fileResults = $duf->upload();
Using filters:
use JeanSouzaK\Duf\Duff; use JeanSouzaK\Duf\Prepare\WebResource; use JeanSouzaK\Duf\Filter\WebFileSizeFilter; use JeanSouzaK\Duf\Filter\WebFileExtensionFilter; use JeanSouzaK\Duf\Filter\LocalFileSizeFilter; use JeanSouzaK\Duf\Filter\LocalFileExtensionFilter; //Call Factory to get google cloud storage duffer instance $duf = Duff::getInstance(Duff::GOOGLE_CLOUD_STORAGE, [ 'project_id' => 'storage-test-227305', 'key_path' => '/home/env/storage-test-227305-8472347a39489.json' ]); //[WEB Filter] Configure a maximum file size to bind in a resource $webPngSizeFilter = new WebFileSizeFilter(2, WebFileSizeFilter::MB); //[WEB Filter] Configure alloweed extensions to bind in a resource $webAllowedExtensionFilter = new WebFileExtensionFilter([ WebFileExtensionFilter::JPEG, WebFileExtensionFilter::PNG ]); $localJpgSizeFilter = new LocalFileSizeFilter(1, 'M'); $localAllowedExtensionFilter = new LocalFileExtensionFilter([ 'json', 'doc', 'docx' ]); //Prepare object name, url and filters $duf->prepare([ new WebResource('object_one.png', 'https://dummyimage.com/600x400/000/fff', [$webPngSizeFilter]), new WebResource('object_two.png', 'https://dummyimage.com/600x400/000/fff.gif', [$webAllowedExtensionFilter]), new WebResource('object_three.png', 'https://dummyimage.com/100x100/000/fff', [$webPngSizeFilter, $webAllowedExtensionFilter]), new LocalResource('imagem', '/home/test/images/test.jpg', [$localJpgSizeFilter]), new LocalResource('composer.json', '/home/test/project/composer.json', [$localAllowedExtensionFilter]), ]); //Make download prepared files $duf->download(); //Add google cloud storage bucket name $duf->addBucket('meubucket666'); //Upload downloaded files to bucket and get results $fileResults = $duf->upload(); // Iterate results and get result path foreach ($fileResults as $fileResult) { echo (string)$fileResult.'<br/>'; } //You can dump result objects to see more details //Working with result object: /** @var File $fileResult */ foreach ($fileResults as $fileResult) { if($fileResult->hasError()){ echo "File {$fileResult->getName()} contains error: {$fileResult->getErrorMessage()}<br/>"; continue; } echo "File {$fileResult->getName()} uploaded sucessfull<br/>"; } //Write your own filters use JeanSouzaK\Duf\Filter\PathFilterable; use JeanSouzaK\Duf\Filter\HeaderFilterable; use JeanSouzaK\Duf\Filter\Filterable; //Local files filter class MyLocalFilter implements Filterable, PathFilterable { public function applyPathFilters(array $filePathInfo) { //throw exception with message } } //Web files filter class MyWebFilter implements Filterable, HeaderFilterable { public function applyHeaderFilter(array $fileHeadersInfo) { //throw exception with message } }
jeansouzak/duf 适用场景与选型建议
jeansouzak/duf 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.07k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2020 年 05 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jeansouzak/duf 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jeansouzak/duf 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-05-12
