引入 sdk
"qiniu/php-sdk": "^7.2"
使用 sdk
<?php
namespace App\Service;
use Qiniu\Auth;
use Illuminate\Support\Facades\Log;
class Qiniu
{
public static function uploadVoice($bucket, $fileName, $realPath)
{
if( !in_array($bucket, ['voice'])){
return false;
}
$config = config('filesystems.disks.qiniu');
$auth = new Auth($config['access_key'], $config['secret_key']);
$upManager = new \Qiniu\Storage\UploadManager();
$savekey = \Qiniu\base64_urlSafeEncode($bucket.':'.$fileName);
$fops = $config['fops'].'|saveas/'.$savekey;
$policy = [
'persistentOps' => $fops,
'persistentPipeline' => $config['pipeline'],
];
$uptoken = $auth->uploadToken($bucket, null, 3600, $policy);
list($ret, $err) = $upManager->putFile($uptoken, $fileName, $realPath);
if( $err !== null ){
Log::error($err);
return false;
}else{
Log::info($ret);
return $config['bucket'][$bucket]['domain'].$ret['key'];
}
}
}
進行上傳
if( $request->hasFile('voice') )
{
$file = $request->file('voice');
$size = $request->input('size');
if( $file->isValid() ){
$realPath = $file->getRealPath();
$fileName =date('Ymdhis').'/'.uniqid().'.mp3';
$url = Qiniu::uploadVoice('voice',$fileName,$realPath);
if( $url === false ){
return Y::error('語音上傳失敗');
}
}else{
return Y::error('語音上傳失敗');
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結