You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.7 KiB
PHTML

5 years ago
<?php
namespace Base\Service;
use OSS\Core\OssUtil;
use OSS\OssClient;
use OSS\Core\OSsException;
/**
* 目前仅支持阿里云OSS
*/
class OssService {
private $accessKeyId = '';
private $accessKeySecret = '';
private $endpoint = '';
5 years ago
private $isCName = false;
private $bucket = '';
private $domain = '';
5 years ago
private $client;
private $errorMessage = '';
public function __construct()
{
Vendor('OSS.autoload');
$this->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);
5 years ago
}
public function upload($localFilePath, $saveFileName)
{
try {
$this->multiuploadFile($localFilePath, $saveFileName);
return [
'status' => true,
'message' => '上传OSS成功',
'data' => [
5 years ago
'url' => $this->getUrl($saveFileName)
5 years ago
]
];
} 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. 完成上传
*/
5 years ago
$this->client->completeMultipartUpload($this->bucket, $saveFileName, $uploadId, $uploadParts);
5 years ago
}
/**
*删除文件
*/
public function deleteObject($deleteFile)
{
$this->client->deleteObject($this->bucket, $deleteFile);
}
private function getUrl($saveFileName)
{
$url = '';
/* if ($this->isCName) {
$url = 'http://' . $this->endpoint . '/' . $saveFileName;
5 years ago
} else {
$url = 'https://' . $this->bucket . '.' . $this->endpoint . '/' . $saveFileName;
5 years ago
$url = str_replace('-internal', '', $url);
} */
$url = $this->domain . '/' . $saveFileName;
5 years ago
return $url;
5 years ago
}
}