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

一、安装

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,非常方便.

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以通过以下步骤实现: 1. 首先,你需要在 `config/filesystem.php` 文件中添加配置信息,以便连接到阿里云 OSS。 ``` 'disks' => [ // ... 'oss' => [ 'driver' => 'oss', 'access_id' => env('OSS_ACCESS_ID'), 'access_key' => env('OSS_ACCESS_KEY'), 'bucket' => env('OSS_BUCKET'), 'endpoint' => env('OSS_ENDPOINT'), 'isCName' => env('OSS_IS_CNAME', false), 'debug' => env('OSS_DEBUG', false), ], ], ``` 2. 然后,在你的控制器中,使用以下代码从 OSS 下载图片并执行自动下载: ```php use Illuminate\Support\Facades\Storage; public function downloadImage() { $filename = 'your_image_file_name.jpg'; // 替换成你的图片文件名 $path = 'your/oss/directory/' . $filename; // 替换成你的 OSS 目录和文件名 $headers = [ 'Content-Type' => 'image/jpeg', 'Content-Disposition' => 'attachment; filename="' . $filename . '"', ]; return response( Storage::disk('oss')->get($path), 200, $headers ); } ``` 3. 如果你需要修改文件名,可以在下载之前先将图片下载到本地,修改文件名后再返回给浏览器。示例代码如下: ```php use Illuminate\Support\Facades\Storage; public function downloadImage() { $filename = 'your_image_file_name.jpg'; // 替换成你的图片文件名 $path = 'your/oss/directory/' . $filename; // 替换成你的 OSS 目录和文件名 $tempFilename = 'temp_' . $filename; // 临时文件名 $tempPath = storage_path('app/public/temp/' . $tempFilename); // 临时文件路径 // 从 OSS 下载图片到本地 Storage::disk('oss')->download($path, $tempPath); // 修改文件名 $newFilename = 'new_image_file_name.jpg'; // 替换成你的新文件名 $newPath = storage_path('app/public/temp/' . $newFilename); // 替换成你的新文件路径 rename($tempPath, $newPath); // 返回给浏览器 $headers = [ 'Content-Type' => 'image/jpeg', 'Content-Disposition' => 'attachment; filename="' . $newFilename . '"', ]; return response( file_get_contents($newPath), 200, $headers ); } ``` 注意,上面的示例代码中使用了本地磁盘驱动器 `public`,你需要在 `config/filesystem.php` 文件中配置该磁盘驱动器: ``` 'disks' => [ // ... 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL') . '/storage', 'visibility' => 'public', ], ], ``` 另外,你需要在 `storage/app/public` 目录下创建一个 `temp` 目录,用于存放临时文件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值