PHP code example of larva / laravel-flysystem-kodo
1. Go to this page and download the library: Download larva/laravel-flysystem-kodo 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/ */
larva / laravel-flysystem-kodo example snippets
'kodo' => [
'driver' => 'kodo',
'access_key' => env('QINIU_ACCESS_KEY'),
'secret_key' => env('QINIU_SECRET_KEY'),
'bucket' => env('QINIU_BUCKET'),
'url' => env('QINIU_BUCKET_URL'), // CDN 或自定义域名,末尾不要斜杠,如 https://cdn.example.com
'root' => env('QINIU_ROOT', ''), // 存储路径前缀,可选
'is_custom_domain' => false, // 如果 endpoint 是绑定的自定义域名,设置为 true,同时 url 设置无效
'endpoint' => env('QINIU_ENDPOINT', ''), // 自定义域名(当 is_custom_domain 为 true 时使用)
'ssl' => true, // 是否使用 HTTPS
'upload_url' => env('QINIU_UPLOAD_URL', 'https://upload.qiniup.com'), // 上传端点,可选
'visibility' => 'public', // 默认文件可见性:public 或 private
'directory_visibility' => 'public', // 默认目录可见性:public 或 private,可选
'options' => [], // 传递给底层 Kodo 适配器的额外选项,可选
'throw' => false,
'report' => false,
],
'default' => 'kodo',
use Illuminate\Support\Facades\Storage;
// 获取磁盘实例
$disk = Storage::disk('kodo');
// 写入文件
$disk->put('path/to/file.txt', 'file contents');
// 读取文件
$contents = $disk->get('path/to/file.txt');
// 检查文件是否存在
$exists = $disk->exists('path/to/file.txt');
// 删除文件
$disk->delete('path/to/file.txt');
// 复制文件
$disk->copy('source/path.txt', 'dest/path.txt');
// 移动文件
$disk->move('source/path.txt', 'dest/path.txt');
// 列出目录内容
$files = $disk->files('directory');
$allFiles = $disk->allFiles('directory');
// 上传文件
$path = $disk->putFile('uploads', $request->file('avatar'));
// 上传文件并指定可见性
$path = $disk->putFile('uploads', $request->file('avatar'), 'public');
// 获取文件 URL
$url = Storage::disk('kodo')->url('path/to/file.txt');
// 获取文件可见性
$visibility = Storage::disk('kodo')->getVisibility('path/to/file.txt');
// 设置文件可见性
Storage::disk('kodo')->setVisibility('path/to/file.txt', 'private');
use Carbon\Carbon;
// 生成临时下载 URL(默认 5 分钟,可自定义)
$tempUrl = Storage::disk('kodo')->temporaryUrl(
'path/to/private-file.txt',
Carbon::now()->addMinutes(30)
);
// 生成临时上传 URL
$result = Storage::disk('kodo')->temporaryUploadUrl(
'path/to/upload.txt',
Carbon::now()->addMinutes(10)
);
// $result['url'] — 上传端点 URL(如 https://upload.qiniup.com)
// $result['headers'] — 上传请求所需的 Authorization 头(包含上传凭证)
use Larva\Flysystem\Qiniu\KodoAdapter;
/** @var KodoAdapter $adapter */
$adapter = Storage::disk('kodo')->getAdapter();
$auth = $adapter->getClient(); // Qiniu\Auth 实例
// routes/api.php
use Illuminate\Support\Facades\Storage;
use Carbon\Carbon;
Route::post('/kodo/upload-url', function (\Illuminate\Http\Request $request) {
$request->validate([
'filename' => '>json([
'url' => $result['url'],
'headers' => $result['headers'],
'path' => $path,
]);
});
'kodo' => [
'driver' => 'kodo',
'access_key' => env('QINIU_ACCESS_KEY'),
'secret_key' => env('QINIU_SECRET_KEY'),
'bucket' => env('QINIU_BUCKET'),
'endpoint' => env('QINIU_ENDPOINT'), // 自定义域名,如 cdn.example.com
'is_custom_domain' => true,
'ssl' => true,
// ...
],