PHP code example of iidestiny / flysystem-oss
1. Go to this page and download the library: Download iidestiny/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/ */
iidestiny / flysystem-oss example snippets
use League\Flysystem\Filesystem;
use Iidestiny\Flysystem\Oss\OssAdapter;
use Iidestiny\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);
void $flysystem->write('file.md', 'contents');
void $flysystem->write('file.md', 'http://httpbin.org/robots.txt', ['options' => ['xxxxx' => 'application/redirect302']]);
void $flysystem->writeStream('file.md', fopen('path/to/your/local/file.jpg', 'r'));
void $flysystem->move('foo.md', 'bar.md');
void $flysystem->copy('foo.md', 'foo2.md');
void $flysystem->delete('file.md');
void $flysystem->deleteDirectory('file.md');
bool $flysystem->fileExists('file.md');
$flysystem->read('file.md');
array $flysystem->listContents();
array $flysystem->getMetadata('file.md');
int $flysystem->fileSize('file.md');
string $adapter->getUrl('file.md'); //
string $flysystem->mimeType('file.md');
int $flysystem->lastModified('file.md');
$adapter = new OssAdapter($accessKeyId, $accessKeySecret, $endpoint, $bucket, $isCName, $prefix);
// 获取 oss 资源访问链接
string $adapter->getUrl('file.md');
// url 访问有效期 & 图片处理「$timeout 为多少秒过期」
// 默认GET
string $adapter->getTemporaryUrl('file.md', $timeout, ['x-oss-process' => 'image/circle,r_100']);
// PUT
string $adapter->getTemporaryUrl('file.md', $timeout, ['x-oss-process' => 'image/circle,r_100'],'PUT');
// 多个bucket切换
$adapter->bucket('test')->has('file.md');
$kernel = $adapter->ossKernel();
// 例如:防盗链功能
$refererConfig = new RefererConfig();
// 设置允许空Referer。
$refererConfig->setAllowEmptyReferer(true);
// 添加Referer白名单。Referer参数支持通配符星号(*)和问号(?)。
$refererConfig->addReferer("www.aliiyun.com");
$refererConfig->addReferer("www.aliiyuncs.com");
$kernel->putBucketReferer($bucket, $refererConfig);
/**
* 1. 前缀如:'images/'
* 2. 回调服务器 url
* 3. 回调自定义参数,oss 回传应用服务器时会带上
* 4. 当前直传配置链接有效期
* 5. 文件大小限制
* 6. 回调系统参数, 默认值: Iidestiny\Flysystem\Oss\OssAdapter::SYSTEM_FIELD
*/
$adapter->signatureConfig($prefix = '/', $callBackUrl = '', $customData = [], $expire = 30, $maxSize = 1024 * 1024 * 2, $systemData = ['etag' => '${etag}', 'filename' => '${object}']);
list($verify, $data) = $adapter->verify();
// [$verify, $data] = $flysystem->verify(); // php 7.1 +
if (!$verify) {
// 验证失败处理,此时 $data 为验签失败提示信息
}
// 注意一定要返回 json 格式的字符串,因为 oss 服务器只接收 json 格式,否则给前端报 CallbackFailed
header("Content-Type: application/json");
echo json_encode($data);