PHP code example of qifen / flysystem-oss
1. Go to this page and download the library: Download qifen/flysystem-oss library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
qifen / flysystem-oss example snippets
use Qifen\FilesystemOss\Plugins\FileUrl;
use Qifen\FilesystemOss\Plugins\SignUrl;
use Qifen\FilesystemOss\Plugins\TemporaryUrl;
use Qifen\FilesystemOss\Plugins\SetBucket;
// 获取 oss 资源访问链接
$flysystem->addPlugin(new FileUrl());
string $flysystem->getUrl('file.md');
// url 访问有效期 & 图片处理「$timeout 为多少秒过期」
$flysystem->addPlugin(new SignUrl());
// 默认GET
string $flysystem->signUrl('file.md', $timeout, ['x-oss-process' => 'image/circle,r_100']);
// PUT方式
string $flysystem->signUrl('file.md', $timeout, ['x-oss-process' => 'image/circle,r_100'],'PUT');
// url 访问有效期「$expiration 为未来时间 2019-05-05 17:50:32」
$flysystem->addPlugin(new TemporaryUrl());
// 默认GET
string $flysystem->getTemporaryUrl('file.md', $expiration);
// PUT方式
string $flysystem->getTemporaryUrl('file.md', $expiration,[],'PUT');
// 多个bucket切换
$flysystem->addPlugin(new SetBucket());
$flysystem->bucket('test')->has('file.md');
use Qifen\FilesystemOss\Plugins\Kernel;
$flysystem->addPlugin(new Kernel());
$kernel = $flysystem->kernel();
// 例如:防盗链功能
$refererConfig = new RefererConfig();
// 设置允许空Referer。
$refererConfig->setAllowEmptyReferer(true);
// 添加Referer白名单。Referer参数支持通配符星号(*)和问号(?)。
$refererConfig->addReferer("www.aliiyun.com");
$refererConfig->addReferer("www.aliiyuncs.com");
$kernel->putBucketReferer($bucket, $refererConfig);
use Qifen\FilesystemOss\Plugins\SignatureConfig;
$flysystem->addPlugin(new SignatureConfig());
/**
* 1. 前缀如:'images/'
* 2. 回调服务器 url
* 3. 回调自定义参数,oss 回传应用服务器时会带上
* 4. 当前直传配置链接有效期
* 5. 文件大小限制
* 6. 回调系统参数, 默认值: Qifen\FilesystemOss\OssAdapter::SYSTEM_FIELD
*/
object $flysystem->signatureConfig($prefix = '/', $callBackUrl = '', $customData = [], $expire = 30, $maxSize = 1024 * 1024 * 2, $systemData = ['etag' => '${etag}', 'filename' => '${object}']);
use Qifen\FilesystemOss\Plugins\Verify;
$flysystem->addPlugin(new Verify());
list($verify, $data) = $flysystem->verify();
// [$verify, $data] = $flysystem->verify(); // php 7.1 +
if (!$verify) {
// 验证失败处理,此时 $data 为验签失败提示信息
}
// 注意一定要返回 json 格式的字符串,因为 oss 服务器只接收 json 格式,否则给前端报 CallbackFailed
header("Content-Type: application/json");
echo json_encode($data);