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.8 KiB
PHTML

5 years ago
<?php
2 years ago
namespace Base\Tool;
5 years ago
use OSS\Core\OssUtil;
2 years ago
use OSS\OssClient as BaseOssClient;
5 years ago
use OSS\Core\OSsException;
/**
2 years ago
* 阿里云OSS
5 years ago
*/
2 years ago
class OssClient implements StorageClient {
5 years ago
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');
2 years ago
$this->client = new BaseOssClient($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) {
2 years ago
$fromPos = $uploadPosition + (integer) $piece[BaseOssClient::OSS_SEEK_TO];
$toPos = (integer) $piece[BaseOssClient::OSS_LENGTH] + $fromPos - 1;
5 years ago
$upOptions = [
2 years ago
BaseOssClient::OSS_FILE_UPLOAD => $uploadFile,
BaseOssClient::OSS_PART_NUM => ($i + 1),
BaseOssClient::OSS_SEEK_TO => $fromPos,
BaseOssClient::OSS_LENGTH => $toPos - $fromPos + 1,
BaseOssClient::OSS_CHECK_MD5 => $isCheckMd5,
5 years ago
];
if ($isCheckMd5) {
$contentMd5 = OssUtil::getMd5SumForFile($uploadFile, $fromPos, $toPos);
2 years ago
$upOptions[BaseOssClient::OSS_CONTENT_MD5] = $contentMd5;
5 years ago
}
// 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
}
/**
*删除文件
*/
2 years ago
public function delete($deleteFile)
5 years ago
{
$this->client->deleteObject($this->bucket, $deleteFile);
}
5 years ago
public function getUrl($saveFileName)
5 years ago
{
$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
}
}