domain = C('COS_DOMAIN'); $this->region = C('COS_REGION'); $this->bucket = C('COS_BUCKET'); $this->secretId = C('COS_SECRET_ID'); $this->secretKey = C('COS_SECRET_KEY'); $this->client = new BaseCosClient( [ 'region' => $this->region, 'scheme' => 'https', //协议头部,默认为http 'credentials'=> [ 'secretId' => $this->secretId, 'secretKey' => $this->secretKey, ] ] ); } public function upload($localFilePath, $saveFileName) { try { $file = fopen($localFilePath, 'rb'); if (!$file) { throw new Exception('读取文件失败'); } $result = $this->client->Upload( $this->bucket, $saveFileName, $file ); return [ 'status' => true, 'message' => '上传成功', 'data' => [ 'url' => $this->getUrl($saveFileName) ] ]; } catch (\Exception $e) { return [ 'status' => false, 'message' => $e->getMessage() ]; } } public function delete($deleteFile) { try { $result = $this->client->deleteObject(array( 'Bucket' => $this->bucket, 'Key' => $deleteFile )); return [ 'status' => true, 'message' => '删除成功', ]; } catch (\Exception $e) { return [ 'status' => false, 'message' => $e->getMessage() ]; } } public function getUrl($saveFileName) { return $this->domain . '/' . $saveFileName; } public function __destruct() { } }