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
PHP
118 lines
3.8 KiB
PHP
<?php
|
|
namespace Base\Tool;
|
|
|
|
use OSS\Core\OssUtil;
|
|
use OSS\OssClient as BaseOssClient;
|
|
use OSS\Core\OSsException;
|
|
|
|
/**
|
|
* 阿里云OSS
|
|
*/
|
|
class OssClient implements StorageClient {
|
|
|
|
private $accessKeyId = '';
|
|
private $accessKeySecret = '';
|
|
private $endpoint = '';
|
|
private $isCName = false;
|
|
private $bucket = '';
|
|
private $domain = '';
|
|
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 BaseOssClient($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[BaseOssClient::OSS_SEEK_TO];
|
|
$toPos = (integer) $piece[BaseOssClient::OSS_LENGTH] + $fromPos - 1;
|
|
$upOptions = [
|
|
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,
|
|
];
|
|
if ($isCheckMd5) {
|
|
$contentMd5 = OssUtil::getMd5SumForFile($uploadFile, $fromPos, $toPos);
|
|
$upOptions[BaseOssClient::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 delete($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;
|
|
}
|
|
} |