作者 开飞机的舒克

测试七牛云自动上传

... ... @@ -69,8 +69,10 @@ class Demo extends Api
*/
public function test3()
{
$filepath = ROOT_PATH.'/uploads/barimages/20230317/00000029.png';
$filename = 'http://rramxwmk1.hb-bkt.clouddn.com/campus1/uploads/20230317/';
$qinu = $this->qiniu($filepath,$filename);
$this->success('上传成功',$qinu);
}
}
... ...
... ... @@ -2,6 +2,7 @@
namespace app\common\controller;
use Qiniu\Auth;
use tinymeng\code\Generate;
class Resource
... ... @@ -18,5 +19,46 @@ class Resource
return $file_path;
}
/*
* 七牛云上传
*/
public function qiniu($filePath = '', $fileName = '')
{
// $filePath=ROOT_PATH.'public'.DS.'uploads'.DS.'20200224'.DS.'img1.jpg';//文件路径
// $fileName='public'.DS.'uploads'.DS.'20200224'.DS.'img1.jpg'; //七牛云上的名字
// $filePath = ROOT_PATH . 'public' . DS . 'uploads' . DS . '20200224' . DS . 'img2.jpg';//文件路径
// $fileName = 'public' . DS . 'uploads' . DS . '20200224' . DS . 'img2.jpg'; //七牛云上的名字,头部不带杠
$config = get_addon_config('qiniu');
$policy = array(
'saveKey' => $fileName,
);
$auth = new Auth($config['accessKey'], $config['secretKey']);
$token = $auth->uploadToken($config['bucket'], null, $config['expire'], $policy);
$multipart = [
['name' => 'token', 'contents' => $token],
[
'name' => 'file',
'contents' => fopen($filePath, 'r'),
'filename' => $fileName,
]
];
try {
$client = new \GuzzleHttp\Client();
$res = $client->request('POST', $config['uploadurl'], [
'multipart' => $multipart
]);
$code = $res->getStatusCode();
//成功不做任何操作
} catch (\GuzzleHttp\Exception\ClientException $e) {
// unlink($filePath);
$this->error("上传失败");
}
// dump($code);
// $url = '/';
//上传成功后将存储变更为qiniu
// $this->success("上传成功", ['url' => cdnurl($url), 'save_path' => $url]);
}
}
\ No newline at end of file
... ...