mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-22 19:44:02 +08:00
* 更新版本号 * 完善后台今日统计,增加权限白名单,增加后台首页菜单,调整后台登录页样式 * Merge branch 'koogua/I1XFCF' of https://gitee.com/koogua/course-tencen… * 前台学习资料部分完成 * !2 后台运营统计合并 * 后台学习资料部分完成 * Merge branch 'master' into develop * Merge branch 'master' of https://github.com/xiaochong0302/course-tencent-cloud * 1.增加changelog.md * 1.简化部分路由地址 * Merge pull request #2 from xiaochong0302/dependabot/composer/symfony/h… * Bump symfony/http-foundation from 4.3.4 to 5.1.6
288 lines
6.6 KiB
PHP
288 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Phalcon\Logger\Adapter\File as FileLogger;
|
|
use Qcloud\Cos\Client as CosClient;
|
|
use TencentCloud\Common\Credential;
|
|
use TencentCloud\Common\Exception\TencentCloudSDKException;
|
|
use TencentCloud\Common\Profile\ClientProfile;
|
|
use TencentCloud\Common\Profile\HttpProfile;
|
|
use TencentCloud\Sts\V20180813\Models\GetFederationTokenRequest;
|
|
use TencentCloud\Sts\V20180813\Models\GetFederationTokenResponse;
|
|
use TencentCloud\Sts\V20180813\StsClient;
|
|
|
|
class Storage extends Service
|
|
{
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $settings;
|
|
|
|
/**
|
|
* @var FileLogger
|
|
*/
|
|
protected $logger;
|
|
|
|
/**
|
|
* @var CosClient
|
|
*/
|
|
protected $client;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->settings = $this->getSettings('cos');
|
|
|
|
$this->logger = $this->getLogger('storage');
|
|
|
|
$this->client = $this->getCosClient();
|
|
}
|
|
|
|
/**
|
|
* 获取临时凭证
|
|
*
|
|
* @return GetFederationTokenResponse
|
|
*/
|
|
public function getFederationToken()
|
|
{
|
|
$secret = $this->getSettings('secret');
|
|
|
|
$resource = sprintf('qcs::cos:%s:uid/%s:%s/*',
|
|
$this->settings['region'],
|
|
$secret['app_id'],
|
|
$this->settings['bucket']
|
|
);
|
|
|
|
$policy = json_encode([
|
|
'version' => '2.0',
|
|
'statement' => [
|
|
'effect' => 'allow',
|
|
'action' => [
|
|
'name/cos:PutObject',
|
|
'name/cos:PostObject',
|
|
'name/cos:InitiateMultipartUpload',
|
|
'name/cos:ListMultipartUploads',
|
|
'name/cos:ListParts',
|
|
'name/cos:UploadPart',
|
|
'name/cos:CompleteMultipartUpload',
|
|
],
|
|
'resource' => [$resource],
|
|
],
|
|
]);
|
|
|
|
try {
|
|
|
|
$credential = new Credential($secret['secret_id'], $secret['secret_key']);
|
|
|
|
$httpProfile = new HttpProfile();
|
|
|
|
$httpProfile->setEndpoint('sts.tencentcloudapi.com');
|
|
|
|
$clientProfile = new ClientProfile();
|
|
|
|
$clientProfile->setHttpProfile($httpProfile);
|
|
|
|
$client = new StsClient($credential, $this->settings['region'], $clientProfile);
|
|
|
|
$request = new GetFederationTokenRequest();
|
|
|
|
$params = json_encode([
|
|
'Name' => 'foo',
|
|
'Policy' => urlencode($policy),
|
|
]);
|
|
|
|
$request->fromJsonString($params);
|
|
|
|
return $client->GetFederationToken($request);
|
|
|
|
} catch (TencentCloudSDKException $e) {
|
|
|
|
$this->logger->error('Get Tmp Token Exception' . kg_json_encode([
|
|
'code' => $e->getCode(),
|
|
'message' => $e->getMessage(),
|
|
]));
|
|
|
|
throw new \Exception('Get Tmp Token Exception');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 上传字符内容
|
|
*
|
|
* @param string $key
|
|
* @param string $body
|
|
* @return string|bool
|
|
*/
|
|
public function putString($key, $body)
|
|
{
|
|
$bucket = $this->settings['bucket'];
|
|
|
|
try {
|
|
|
|
$response = $this->client->upload($bucket, $key, $body);
|
|
|
|
$result = $response['Location'] ? $key : false;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->error('Put String Exception ' . kg_json_encode([
|
|
'code' => $e->getCode(),
|
|
'message' => $e->getMessage(),
|
|
]));
|
|
|
|
$result = false;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 上传文件
|
|
*
|
|
* @param string $key
|
|
* @param string $filename
|
|
* @return mixed string|bool
|
|
*/
|
|
public function putFile($key, $filename)
|
|
{
|
|
$bucket = $this->settings['bucket'];
|
|
|
|
try {
|
|
|
|
$body = fopen($filename, 'rb');
|
|
|
|
$response = $this->client->upload($bucket, $key, $body);
|
|
|
|
$result = $response['Location'] ? $key : false;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->error('Put File Exception ' . kg_json_encode([
|
|
'code' => $e->getCode(),
|
|
'message' => $e->getMessage(),
|
|
]));
|
|
|
|
$result = false;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 删除文件
|
|
*
|
|
* @param string $key
|
|
* @return string|bool
|
|
*/
|
|
public function deleteObject($key)
|
|
{
|
|
$bucket = $this->settings['bucket'];
|
|
|
|
try {
|
|
|
|
$response = $this->client->deleteObject([
|
|
'Bucket' => $bucket,
|
|
'Key' => $key,
|
|
]);
|
|
|
|
$result = $response['Location'] ? $key : false;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->error('Delete Object Exception ' . kg_json_encode([
|
|
'code' => $e->getCode(),
|
|
'message' => $e->getMessage(),
|
|
]));
|
|
|
|
$result = false;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 获取文件URL
|
|
*
|
|
* @param string $key
|
|
* @return string
|
|
*/
|
|
public function getFileUrl($key)
|
|
{
|
|
return $this->getBaseUrl() . $key;
|
|
}
|
|
|
|
/**
|
|
* 获取图片URL
|
|
*
|
|
* @param string $key
|
|
* @param string $style
|
|
* @return string
|
|
*/
|
|
public function getImageUrl($key, $style = null)
|
|
{
|
|
$style = $style ?: '';
|
|
|
|
return $this->getBaseUrl() . $key . $style;
|
|
}
|
|
|
|
/**
|
|
* 获取基准URL
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getBaseUrl()
|
|
{
|
|
$protocol = $this->settings['protocol'];
|
|
$domain = $this->settings['domain'];
|
|
|
|
return sprintf('%s://%s', $protocol, trim($domain, '/'));
|
|
}
|
|
|
|
/**
|
|
* 生成文件存储名
|
|
*
|
|
* @param string $extension
|
|
* @param string $prefix
|
|
* @return string
|
|
*/
|
|
protected function generateFileName($extension = '', $prefix = '')
|
|
{
|
|
$randName = date('YmdHis') . rand(100, 999) . rand(100, 999);
|
|
|
|
return $prefix . $randName . '.' . $extension;
|
|
}
|
|
|
|
/**
|
|
* 获取文件扩展名
|
|
*
|
|
* @param $filename
|
|
* @return string
|
|
*/
|
|
protected function getFileExtension($filename)
|
|
{
|
|
$extension = pathinfo($filename, PATHINFO_EXTENSION);
|
|
|
|
return strtolower($extension);
|
|
}
|
|
|
|
/**
|
|
* 获取CosClient
|
|
*
|
|
* @return CosClient
|
|
*/
|
|
protected function getCosClient()
|
|
{
|
|
$secret = $this->getSettings('secret');
|
|
|
|
return new CosClient([
|
|
'region' => $this->settings['region'],
|
|
'schema' => $this->settings['protocol'],
|
|
'credentials' => [
|
|
'secretId' => $secret['secret_id'],
|
|
'secretKey' => $secret['secret_key'],
|
|
]]);
|
|
}
|
|
|
|
}
|