PHP code example of telanflow / flysystem-oss
1. Go to this page and download the library: Download telanflow/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/ */
telanflow / flysystem-oss example snippets
use League\Flysystem\Filesystem;
use Telanflow\Flysystem\Oss\OssAdapter;
use Telanflow\Flysystem\Oss\Plugins\FileUrl;
$prefix = ''; // 前缀,非必填
$accessKeyId = 'xxxxxx';
$accessKeySecret = 'xxxxxx';
$endpoint= 'oss.iidestiny.com'; // ssl:https://iidestiny.com
$bucket = 'bucket';
$isCName = true; // 如果 isCname 为 false,endpoint 应配置 oss 提供的域名如:`oss-cn-beijing.aliyuncs.com`,cname 或 cdn 请自行到阿里 oss 后台配置并绑定 bucket
$adapter = new OssAdapter($accessKeyId, $accessKeySecret, $endpoint, $bucket, $isCName, $prefix);
// $adapter->setCdnUrl('https://cdn.iidestiny.com'); // 设置cdn url
$flysystem = new Filesystem($adapter);
bool $flysystem->write('file.md', 'contents');
bool $flysystem->write('file.md', 'http://httpbin.org/robots.txt', ['options' => ['xxxxx' => 'application/redirect302']]);
bool $flysystem->writeStream('file.md', fopen('path/to/your/local/file.jpg', 'r'));
bool $flysystem->update('file.md', 'new contents');
bool $flysystem->updateStream('file.md', fopen('path/to/your/local/file.jpg', 'r'));
bool $flysystem->rename('foo.md', 'bar.md');
bool $flysystem->copy('foo.md', 'foo2.md');
bool $flysystem->delete('file.md');
bool $flysystem->has('file.md');
string|false $flysystem->read('file.md');
array $flysystem->listContents();
array $flysystem->getMetadata('file.md');
int $flysystem->getSize('file.md');
string $flysystem->getAdapter()->getUrl('file.md');
string $flysystem->getMimetype('file.md');
int $flysystem->getTimestamp('file.md');
use Telanflow\Flysystem\Oss\Plugins\FileUrl;
use Telanflow\Flysystem\Oss\Plugins\SignUrl;
use Telanflow\Flysystem\Oss\Plugins\TemporaryUrl;
use Telanflow\Flysystem\Oss\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 Telanflow\Flysystem\Oss\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 Telanflow\Flysystem\Oss\Plugins\SignatureConfig;
$flysystem->addPlugin(new SignatureConfig());
/**
* 1. 前缀如:'images/'
* 2. 回调服务器 url
* 3. 回调自定义参数,oss 回传应用服务器时会带上
* 4. 当前直传配置链接有效期
* 5. 文件大小限制
* 6. 回调系统参数, 默认值: Telanflow\Flysystem\Oss\OssAdapter::SYSTEM_FIELD
*/
object $flysystem->signatureConfig($prefix = '/', $callBackUrl = '', $customData = [], $expire = 30, $maxSize = 1024 * 1024 * 2, $systemData = ['etag' => '${etag}', 'filename' => '${object}']);
use Telanflow\Flysystem\Oss\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);