This commit is contained in:
2025-08-19 17:30:51 +08:00
parent fd44965f8a
commit 353c72c602
74 changed files with 10575 additions and 44 deletions

View File

@@ -0,0 +1,116 @@
<?php
namespace app\czg\controller;
use app\api\model\CommonInfo;
use app\common\controller\Backend;
use app\common\library\DatabaseRoute;
use OSS\Core\OssException;
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use think\facade\Db;
use think\facade\Log;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class AliossController extends Backend
{
protected array $noNeedLogin = ['*'];
public function upload()
{
$file = request()->file('file');
if(empty($file)) {
$this->error('参数不能为空');
}
$commoninfo = Db::connect(config('database.search_library'));
$endpoint = $commoninfo->name('common_info')->where(['type' => 68])->find()['value'];
$accessKeyId = $commoninfo->name('common_info')->where(['type' => 69])->find()['value'];
$secretAccessKey = $commoninfo->name('common_info')->where(['type' => 70])->find()['value'];
$bucket = $commoninfo->name('common_info')->where(['type' => 71])->find()['value'];
$befor_url = $commoninfo->name('common_info')->where(['type' => 72])->find()['value'];
putenv('OSS_ACCESS_KEY_ID=' . $accessKeyId);
putenv('OSS_ACCESS_KEY_SECRET='. $secretAccessKey);
$provider = new EnvironmentVariableCredentialsProvider();
$object = date('Ymd') . '/' . uniqid() . '.' .$file->getOriginalExtension();
try{
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V1,
);
$ossClient = new OssClient($config);
// 以二进制模式读取文件内容
$handle = fopen($file->getRealPath(), 'rb');
$content = stream_get_contents($handle);
fclose($handle);
// 上传二进制内容明确指定Content-Type
$options = [
OssClient::OSS_CONTENT_LENGTH => strlen($content),
OssClient::OSS_CONTENT_TYPE => $file->getMime(),
// 禁用字符编码转换
'Content-Encoding' => 'binary',
];
$res = $ossClient->putObject($bucket, $object, $content, $options);
Log::write('上传文件结果' . json_encode($res));
if(!empty($res['info'])) {
return $this->ApiDataReturn(['data' => $befor_url . '/' . $object, 'msg' => 'success', 'code' => 0]);
}else {
$this->error('上传失败');
}
} catch(OssException $e) {
$this->error($e->getMessage());
}
}
// 获取阿里云oss存储相关配置
public function getCredentials()
{
$commoninfo = Db::connect(config('database.search_library'));
$configItems = $commoninfo->name('common_info')->whereIn('type', [66, 67, 69, 70])
->column('value', 'type');
AlibabaCloud::accessKeyClient($configItems[69], $configItems[70])
->regionId('cn-nanjing')
->asDefaultClient();
try {
$result = AlibabaCloud::rpc()->product('Sts')
->options([
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V1
])
->version('2015-04-01')
->action('AssumeRole')
->method('POST')
->host($configItems[66]) // Endpoint
->scheme('https')
->options([
'query' => [
'RoleArn' => $configItems[67], // 角色ARN
'RoleSessionName' => 'test', // 会话名称
'DurationSeconds' => 3600, // 凭证有效期(秒)
],
])
->request();
// 获取响应中的凭证信息。
$credentials = $result['Credentials'];
$this->n_success(['data' => [
'accessKeyId' => $credentials['AccessKeyId'],
'accessKeySecret' => $credentials['AccessKeySecret'],
'expiration' => $credentials['Expiration'],
'securityToken' => $credentials['SecurityToken'],
]]);
} catch (ClientException $e) {
// 处理客户端异常。
echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
// 处理服务端异常。
echo $e->getErrorMessage() . PHP_EOL;
}
}
}