master
parent
4b6889d365
commit
5aab375f7c
@ -0,0 +1,109 @@
|
||||
<?php
|
||||
namespace Base\Service;
|
||||
|
||||
use Obs\ObsClient;
|
||||
|
||||
/**
|
||||
* 目前仅支持阿里云OSS
|
||||
*/
|
||||
class OssService
|
||||
{
|
||||
const MAX_PART_SIZE = 5368709120;
|
||||
const MID_PART_SIZE = 10485760;
|
||||
const MIN_PART_SIZE = 102400;
|
||||
|
||||
private $accessKey;
|
||||
private $secretKey;
|
||||
private $endpoint;
|
||||
private $bucket;
|
||||
private $domain;
|
||||
private $client;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->accessKey = C('OBS_ACCESS_KEY');
|
||||
$this->secretKey = C('OBS_SECRET_KEY');
|
||||
$this->endpoint = C('OBS_ENDPOINT');
|
||||
$this->domain = C('OBS_DOMAIN');
|
||||
$this->isCName = C('OBS_IS_CNAME');
|
||||
$this->bucket = C('OBS_BUCKET');
|
||||
$this->client = new ObsClient($this->accessKey, $this->secretKey, $this->endpoint);
|
||||
}
|
||||
|
||||
public function upload($localFilePath, $saveFileName)
|
||||
{
|
||||
/* $resp = $this->client->putObject([
|
||||
'Bucket' => $this->bucket,
|
||||
'Key' => $saveFileName,
|
||||
'SourceFile' => $localFilePath,
|
||||
]); */
|
||||
try {
|
||||
$this->multiuploadFile($localFilePath, $saveFileName);
|
||||
return [
|
||||
'status' => true,
|
||||
'message' => '上传OSS成功',
|
||||
'data' => [
|
||||
'url' => $this->getUrl($saveFileName)
|
||||
]
|
||||
];
|
||||
} catch (Obs\Common\ObsException $e) {
|
||||
return [
|
||||
'status' => false,
|
||||
'message' => $e->getExceptionMessage()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
private function multiuploadFile($file, $saveFileName)
|
||||
{
|
||||
$resp = $this->client->initiateMultipartUpload([
|
||||
'Bucket' => $this->bucket,
|
||||
'Key' => $saveFileName,
|
||||
'ContentType' => 'text/plain'
|
||||
]);
|
||||
$uploadId = $resp['UploadId'];
|
||||
|
||||
$packetSize = 1048576;
|
||||
$byteTotal = filesize($file);
|
||||
$packetTotal = ceil($byteTotal / $packetSize);
|
||||
$fileContent = fopen($file, 'r');
|
||||
$parts = [];
|
||||
for ($i=0; $i<$packetTotal; $i++) {
|
||||
$packetFileContent = fread($fileContent, $packetSize);
|
||||
$resp = $this->client->uploadPart([
|
||||
'Bucket' => $this->bucket,
|
||||
'Key' => $saveFileName,
|
||||
'UploadId' => $uploadId,
|
||||
'PartNumber' => $i+1,
|
||||
'Body' => $packetFileContent
|
||||
]);
|
||||
$parts[] = ['PartNumber' => $i+1, 'ETag' => $resp['ETag']];
|
||||
}
|
||||
fclose($fileContent);
|
||||
|
||||
$resp = $this->client->completeMultipartUpload([
|
||||
'Bucket' => $this->bucket,
|
||||
'Key' => $saveFileName,
|
||||
'UploadId' => $uploadId,
|
||||
'Parts' => $parts
|
||||
]);
|
||||
}
|
||||
|
||||
public function deleteObject($deleteFile)
|
||||
{
|
||||
$resp = $this->client->deleteObject([
|
||||
'Bucket' => $this->bucket,
|
||||
'Key' => $deleteFile,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getUrl($saveFileName)
|
||||
{
|
||||
return $this->domain . '/' . $saveFileName;
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$this->client->close();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue