first commit

This commit is contained in:
编码猿
2024-09-27 01:32:49 +08:00
commit 28ebe05a64
7399 changed files with 1174529 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
<?php
namespace app\common\service;
use think\facade\Env;
class AliyunOssService{
public function getStsToken($data=[]) {
$upload_type = $data['upload_type']??'';
$result['errcode']=0;
$result['errmsg']='成功获取STS凭证';
$nowtime=time();
$sts_expire_time=session('sts_expire_time');
if ($sts_expire_time && $nowtime < $sts_expire_time) {
$result['data']=session('sts_data');
} else {
require_once Env::get('extend_path').'Aliyun/core/Config.php';
$iClientProfile = \DefaultProfile::getProfile("cn-hangzhou", "LTAI8yWB5JgvunlT", "F7OWOL7OA1awQ6tELawWBKTQM5I32U");
$client = new \DefaultAcsClient($iClientProfile);
// 角色资源描述符在RAM的控制台的资源详情页上可以获取
$roleArn = "acs:ram::1845636578658781:role/develop-files-role";
$bucketname='ydyd-develop-file';
if ($upload_type=='chat') {
$bucketname='ydyd-chat-file';
}
if ($upload_type =='product') {
$bucketname='ydyd-product-file';
}
$policy = [];
$policy['Statement'][0]['Action']=array("oss:PutObject","oss:GetObject","oss:ListParts","oss:UploadPart");
$policy['Statement'][0]['Effect']="Allow";
$policy['Statement'][0]['Resource']="acs:oss:*:1845636578658781:".$bucketname."/*";
$policy['Version']="1";
require_once Env::get('extend_path').'Aliyun/sts/AssumeRoleRequest.php';
$request = new \AssumeRoleRequest();
// RoleSessionName即临时身份的会话名称用于区分不同的临时身份
// 您可以使用您的客户的ID作为会话名称
$request->setRoleSessionName("fortest");
$request->setRoleArn($roleArn);
$request->setPolicy(json_encode($policy));
$request->setDurationSeconds(3600);
$sts_expire_time=time()+1800;
$response = $client->getAcsResponse($request);
if ($response->Credentials->AccessKeySecret) {
$result['data']['Region']='oss-cn-hangzhou';
$result['data']['AccessKeySecret']=$response->Credentials->AccessKeySecret;
$result['data']['AccessKeyId']=$response->Credentials->AccessKeyId;
$result['data']['Bucket']=$bucketname;
$result['data']['SecurityToken']=$response->Credentials->SecurityToken;
//结果写入session缓存
session('sts_expire_time',$sts_expire_time);
session('sts_data',$result['data']);
} else {
$result['errcode']=-2;
$result['errmsg']='获取STS凭证失败';
}
}
return $result;
}
}