accessKey = $accessKey; return $this; } public function secretKey(string $secretKey) { $this->secretKey = $secretKey; return $this; } public function bucket(string $bucket) { $this->bucket = $bucket; return $this; } /** * 获取配置信息 * @return $this */ private function getConfig() { $this->accessKey = $this->app->config->get('dtapp.qiniu.kodo.access_key'); $this->secretKey = $this->app->config->get('dtapp.qiniu.kodo.secret_key'); $this->bucket = $this->app->config->get('dtapp.qiniu.kodo.bucket'); return $this; } /** * 上传文件 * @param $object * @param $filePath * @return bool * @throws Exception */ public function upload(string $object, string $filePath) { if (empty($this->accessKey)) $this->getConfig(); if (empty($this->secretKey)) $this->getConfig(); if (empty($this->bucket)) $this->getConfig(); // 初始化签权对象 $auth = new Auth($this->accessKey, $this->secretKey); // 生成上传Token $token = $auth->uploadToken($this->bucket); // 构建 UploadManager 对象 $uploadMgr = new UploadManager(); // 调用 UploadManager 的 putFile 方法进行文件的上传。 list($ret, $err) = $uploadMgr->putFile($token, $object, $filePath); if ($err !== null) return false; else return $this->app->config->get('dtapp.qiniu.kodo.url') . $object; } }