isszz/think-filesystem-driver

thinkphp flysystem 驱动, 本组件需要按需自行安装依赖

v0.01 2021-03-28 16:50 UTC

This package is auto-updated.

Last update: 2024-10-29 06:36:17 UTC


README

安装, 请选择安装依赖组件

七牛依赖基础组件

composer require overtrue/flysystem-qiniu -vvv

阿里云 oss 依赖基础组件

composer require iidestiny/flysystem-oss -vvv

腾讯 cos 依赖基础组件

composer require overtrue/flysystem-cos -vvv

本组件

composer require isszz/think-filesystem-driver -vvv

配置

在 config/filesystem.php 的 disks 里更改配置, 或者在.env内新增相关配置字段

<?php

return [
   'disks' => [
		// 七牛配置
		'qiniu' => [
			'type'       => 'qiniu',
			'access_key' => env('qiniu.access_key', 'xxxxxxxxxxxxxxxx'),
			'secret_key' => env('qiniu.secret_key', 'xxxxxxxxxxxxxxxx'),
			'bucket'     => env('qiniu.bucket', 'test'),
			'domain'     => env('qiniu.domain', 'xxx.xxx.top'), // or host: https://xxxx.xxx.top
		],
		// 阿里 oss 配置
		'oss' => [
			'type'       => 'oss',
			'access_key' => env('oss.access_key', 'xxxxxxxxxxxxxxxx'),
			'secret_key' => env('oss.secret_key', 'xxxxxxxxxxxxxxxx'),
			'bucket'     => env('oss.bucket', 'test'),
			'domain'     => env('oss.domain', 'xxx.xxx.top'), // ssl:https://xxxx.xxx.top
			'is_cname'   => env('oss.is_cname', false), // 如果 is_cname 为 false, domain 应配置 oss 提供的域名如:`oss-cn-beijing.aliyuncs.com`,cname 或 cdn 请自行到阿里 oss 后台配置并绑定 bucket
			'root'       => env('oss.root', ''), // 前缀,非必填
		],
		// 腾讯 cos 配置
		'cos' => [
			'type'			  => 'cos',
			'region'          => env('cos.region', 'ap-shanghai'), // 地域
			'credentials'     => [
				'appId'		  => env('cos.app_id'), // 域名中数字部分
				'secretId'	  => env('cos.secret_id'),
				'secretKey'	  => env('cos.secret_key'),
			],
			'bucket'          => env('cos.bucket'),
			'cdn'             => env('cos.cdn'), // CDN 域名
			'scheme'          => env('cos.scheme', 'https'),
			'read_from_cdn'   => env('cos.read_from_cdn', false),
			'timeout'         => env('cos.timeout', 60),
			'connect_timeout' => env('cos.connect_timeout', 60),
		],
		// ...
   ]
];

.env的配置参考(生产模式建议删除.env)

[QINIU]
ACCESS_KEY = lekrgwlekgjwlekgjlmbdfbhererlkgjerlkgerl
SECRET_KEY = 4hjf9048t3823hfg8230984u9fj02389urg234ti
BUCKET = cfyun-img
DOMAIN = img.cfyun.cc

[OSS]
ACCESS_KEY = lekrgwlekgjwlekgjlmbdfbhererlkgjerlkgerl
SECRET_KEY = 4hjf9048t3823hfg8230984u9fj02389urg234ti
BUCKET = cfyun-img
DOMAIN = img.cfyun.cc
IS_NAME = false

[COS]
APP_ID = lekrgwlekgjwlekgjlmbdfbhererlkgjerlkgerl
SECRET_ID = 4hjf9048t3823hfg8230984u9fj02389urg234ti
SECRET_KEY = 4hjf9048t3823hfg8230984u9fj02389urg234ti
BUCKET = cfyun-img
cdn = img.cfyun.cc

基础用法 qiniu, oss, cos 并无差别

use think\facade\Filesystem;

$disk = Filesystem::disk('qiniu');
// $disk = Filesystem::disk('oss');
// $disk = Filesystem::disk('cos');

// 按需引入插件, cos暂时无用
$disk->addPlugin();

$file = request()->file('file');

$filepath = $disk->putFile('mypath', $file);

// 插件提供的url获取
$url = $disk->getUrl($filepath);

dd([$filepath, $url]); 

html

<form action="{{ url('index/upload') }}" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <button type="submit">上传</button>
</form>