laravel使用腾讯云cos存储对象保存文件

安装依赖

composer require freyo/flysystem-qcloud-cos-v5

添加服务

修改config/app.php文件,添加cos服务

Freyo\Flysystem\QcloudCOSv5\ServiceProvider::class,

添加文件存储驱动

修改config/filesystems.php文件,添加cos配置

'disks'=>[
    // ...
    'cosv5' => [
          'driver' => 'cosv5',
          'region'          => env('COSV5_REGION', 'ap-guangzhou'),
          'credentials'     => [
              'appId'     => env('COSV5_APP_ID'),
              'secretId'  => env('COSV5_SECRET_ID'),
              'secretKey' => env('COSV5_SECRET_KEY'),
          ],
          'timeout'         => env('COSV5_TIMEOUT', 60),
          'connect_timeout' => env('COSV5_CONNECT_TIMEOUT', 60),
          'bucket'          => env('COSV5_BUCKET'),
          'cdn'             => env('COSV5_CDN'),
          'scheme'          => env('COSV5_SCHEME', 'https'),
    ],
],

添加配置

修改.env文件

COSV5_REGION=xxx
COSV5_APP_ID=xxx
COSV5_SECRET_ID=xxx
COSV5_SECRET_KEY=xxx
COSV5_TIMEOUT=xxx
COSV5_CONNECT_TIMEOUT=xxx
COSV5_BUCKET=xxx
COSV5_CDN=xxx
COSV5_SCHEME=xxx

编写调用存储代码

$filename = $this->getFileName($request->image);获取文件名

$FilePath = $request->image->getRealPath(); //获取文件临时存放位置

Storage::disk('cosv5')->put($filename, file_get_contents($FilePath)); //云存储文件

$request->image->move(base_path('public/images'), $filename);//本地保存文件