准备工作
1.注册七牛账号,并且完成实名认证
2.安装 composer
步骤一 (安装对应的SDK)
composer require qiniu/php-sdk
步骤二 (建立文件app/Http/Helpers/qiuniu.php)并写入以下信息
<?php
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
function qiniuUpload($filePath){
// 用于签名的公钥和私钥
$accessKey = 'xxxxx';
$secretKey = 'xxxxx';
// 初始化签权对象
$auth = new Auth($accessKey, $secretKey);
$bucket = 'test';
// 生成上传Token
$token = $auth->uploadToken($bucket);
//上传七牛保存的文件名
$key = basename($filePath);
// 构建 UploadManager 对象
$uploadMgr = new UploadManager();
//进行文件上传
$uploadMgr->putFile($token,$key,$filePath);
unlink($filePath);
return config('qiniu.qiniuUrl').'/'.$key;
}
步骤三 (composer.json中添加以下信息) 并且终端行: composer dump-autoload
步骤四(在控制器中测试文件上传)
public function qiniuUpload(Request $request){
//return public_path('/');
$path = $request->file('image')->store('images','public');
$realPath = public_path($path);
return qiniuUpload($realPath);
}