accessKeyId = C('OSS_ACCESS_KEY_ID'); $this->accessKeySecret = C('OSS_ACCESS_KEY_SECRET'); $this->endpoint = C('OSS_ENDPOINT'); $this->domain = C('OSS_DOMAIN'); $this->isCName = C('OSS_IS_CNAME'); $this->bucket = C('OSS_BUCKET'); $this->client = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint, $this->isCName); } public function upload($localFilePath, $saveFileName) { try { $this->multiuploadFile($localFilePath, $saveFileName); return [ 'status' => true, 'message' => '上传OSS成功', 'data' => [ 'url' => $this->getUrl($saveFileName) ] ]; } catch (OssException $e) { return [ 'status' => false, 'message' => $e->getMessage() ]; } } private function multiuploadFile($file, $saveFileName) { $uploadId = $this->client->initiateMultipartUpload($this->bucket, $saveFileName); /* * step 2. 上传分片 */ $partSize = 5 * 1000 * 1024; $uploadFile = $file; $uploadFileSize = filesize($uploadFile); $pieces = $this->client->generateMultiuploadParts($uploadFileSize, $partSize); $responseUploadPart = []; $uploadPosition = 0; $isCheckMd5 = true; foreach ($pieces as $i => $piece) { $fromPos = $uploadPosition + (integer) $piece[OssClient::OSS_SEEK_TO]; $toPos = (integer) $piece[OssClient::OSS_LENGTH] + $fromPos - 1; $upOptions = [ OssClient::OSS_FILE_UPLOAD => $uploadFile, OssClient::OSS_PART_NUM => ($i + 1), OssClient::OSS_SEEK_TO => $fromPos, OssClient::OSS_LENGTH => $toPos - $fromPos + 1, OssClient::OSS_CHECK_MD5 => $isCheckMd5, ]; if ($isCheckMd5) { $contentMd5 = OssUtil::getMd5SumForFile($uploadFile, $fromPos, $toPos); $upOptions[OssClient::OSS_CONTENT_MD5] = $contentMd5; } // 2. 将每一分片上传到OSS $responseUploadPart[] = $this->client->uploadPart($this->bucket, $saveFileName, $uploadId, $upOptions); } $uploadParts = []; foreach ($responseUploadPart as $i => $eTag) { $uploadParts[] = [ 'PartNumber' => ($i + 1), 'ETag' => $eTag, ]; } /** * step 3. 完成上传 */ $this->client->completeMultipartUpload($this->bucket, $saveFileName, $uploadId, $uploadParts); } /** *删除文件 */ public function deleteObject($deleteFile) { $this->client->deleteObject($this->bucket, $deleteFile); } public function getUrl($saveFileName) { $url = ''; /* if ($this->isCName) { $url = 'http://' . $this->endpoint . '/' . $saveFileName; } else { $url = 'https://' . $this->bucket . '.' . $this->endpoint . '/' . $saveFileName; $url = str_replace('-internal', '', $url); } */ $url = $this->domain . '/' . $saveFileName; return $url; } }