tp6.1 第三方filesystem扩展支持阿里云OSS和七牛和腾讯云COS)使用教程

本文介绍了如何将ThinkPHP从6.0升级到6.1.2,并详细展示了在升级后如何更换和配置第三方云存储插件,包括阿里云、七牛云等。同时,提供了错误代码的修正以及正确使用yzh52521/think-filesystem和thans/think-filesystem插件的方法,包含上传文件的示例代码。
摘要由CSDN通过智能技术生成

一、安装

1.1 TP6.0时安装的,现在升级到TP6.1.2版本了,这个插件没有更新,换其它插件(不推荐,停用)

官网:thinkphp-filesystem-cloud: thinkphp6.0的第三方filesystem扩展包,支持上传阿里云、七牛云。

composer require thans/thinkphp-filesystem-cloud

1.2 tp6.1.2版本用yzh52521/think-filesystem这个

官网:yzh52521/think-filesystem · ThinkPHP

composer require yzh52521/think-filesystem

二: 在config/filesystem.php中添加配置(两个插件都要这个)

        'local'  => [
            'type' => 'local',
            'root' => app()->getRuntimePath() . 'upload',
        ],
        'public' => [
            // 磁盘类型
            'type'       => 'local',
            // 磁盘路径
            'root'       => app()->getRootPath() . 'public/upload',
            // 磁盘路径对应的外部URL路径
            'url'        => '/upload',
            // 可见性 不能改
            'visibility' => 'public',
],
'aliyun' => [
    'type'         => 'aliyun',
    'accessId'     => '******',
    'accessSecret' => '******',
    'bucket'       => 'bucket',
    'endpoint'     => 'oss-cn-hongkong.aliyuncs.com',
    'url'          => 'http://oss-cn-hongkong.aliyuncs.com',
],
'qiniu'  => [
    'type'      => 'qiniu',
    'accessKey' => '******',
    'secretKey' => '******',
    'bucket'    => 'bucket',
    'domain'    => 'https://youcdn.domain.com',
],
'qcloud' => [
    'type'       => 'qcloud',
    'region'      => '***', //bucket 所属区域 英文
    'app_id'      => '***', // 域名中数字部分
    'secret_id'   => '***',
    'secret_key'  => '***',
    'bucket'          => '***',
    'timeout'         => 60,
    'connect_timeout' => 60,
    'cdn'             => '您的 CDN 域名',
    'scheme'          => 'https',
    'read_from_cdn'   => false,
]
'obs'=>[
      'type' =>'obs',
      'root' => '',
      'key' => env('OBS_KEY'),
      'secret' => env('OBS_SECRET'),
      'bucket' => env('OBS_BUCKET'),
      'endpoint' => env('OBS_ENDPOINT'),
      'is_cname' => env('OBS_IS_CNAME', false), //true or false...
      'security_token' => env('OBS_SECURITY_TOKEN'),//true or false...
],
's3'=>[
       'type' =>'s3',
      'credentials'             => [
                'key'    => 'S3_KEY',
                'secret' => 'S3_SECRET',
      ],
      'region'                  => 'S3_REGION',
      'version'                 => 'latest',
      'bucket_endpoint'         => false,
      'use_path_style_endpoint' => false,
      'endpoint'                => 'S3_ENDPOINT',
      'bucket_name'             => 'S3_BUCKET',
],
'google'=>[
    'type' =>'google',
    'projectId' => 'GOOGLE_PROJECT_ID',//your-project-id
    'bucket' => 'GOOGLE_BUCKET', //your-bucket-name
    'prefix' => '', //optional-prefix 
],
'sftp'=>[
    'type' =>'sftp',
    'host' => 'example.com',
    // 基于基础的身份验证设置...
    'username' => 'username',
    'password' => 'password',
    // 使用加密密码进行基于 SSH 密钥的身份验证的设置...
    'privateKey' => null,
    'passphrase' => null,
    // 可选的 SFTP 设置
    'port' => 22,
    'root' => '/path/to/root',
    'url' => '/path/to/root',
    'timeout' => 10
]

上面是修正正确代码,官方代码中是有错误的

   

'accessKey' => '******',
'secretKey' => '******',

 

三: yzh52521/think-filesystem插件使用方法

官方demo

$file = $this->request->file( 'image' );
      try {
            validate(
                ['image' => [
                        // 限制文件大小(单位b),这里限制为4M
                        'fileSize' => 10 * 1024 * 1000,
                        // 限制文件后缀,多个后缀以英文逗号分割
                        'fileExt'  => 'gif,jpg,png,jpeg'
                    ]
                ])->check( ['image' => $file] );

            $path     = \yzh52521\filesystem\facade\Filesystem::disk( 'public' )->putFile( 'test',$file);
            $url      = \yzh52521\filesystem\facade\Filesystem::disk( 'public' )->url( $path );
            return json( ['path' => $path,'url'  => $url] );
      } catch ( \think\exception\ValidateException $e ) {
            echo $e->getMessage();
     }

$path     = \yzh52521\filesystem\facade\Filesystem::disk( 'public' )->putFile( 'test',$file);
 $url      = \yzh52521\filesystem\facade\Filesystem::disk( 'public' )->url( $path );

按官方上面的代码报错:

 正确方法:

$path  =(new \yzh52521\filesystem\Filesystem(app()))->disk($public_path)->putFile($path,$files);
$saveName = (new \yzh52521\filesystem\Filesystem(app()))->disk($public_path)->url($path);

use yzh52521\filesystem\Filesystem ;


$file = new Filesystem(app());
$path  = $file->disk($public_path)->putFile($path,$files);
$saveName = $file->disk($public_path)->url($path);

如果还不行,再安装官网这个,

composer require topthink/think-filesystem

测试成功环境是两个都安装

四: thans插件使用方法

使用(Filesystem::disk('public')修改为Filesystem::disk('aliyun'))

请参考thinkphp文档 文档地址:https://www.kancloud.cn/manual/thinkphp6_0/1037639

//TP6 上传本地服务器
$savename = \think\facade\Filesystem::disk('public')->putFile( 'topic', $file);

//扩展包引用方法
$savename = \think\facade\Filesystem::disk('aliyun')->putFile( 'topic', $file);

$savename = \think\facade\Filesystem::disk('aliyun')->putFile( 'topic', $file); 就是这个名称

只要参数填写正确,只需要修改一个地方就可以直接使用第三方的OSS,非常方便.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值