You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\base\controller\cos\api;
use app\BaseController;
use think\App;
use think\facade\Request;
class Upload extends BaseController
{
/**
* 上传图片
* @date 2022-12-12
*/
public function getCosParam()
{
// $is_original_name = input('post.is_original_name/d', 0);
// $file_path = input('post.file_path/s', '');
$tecent_cos_param = get_tencent_cos_config(UID);
$credentials_class = new \tencent\sts\credentials\Credentials($tecent_cos_param);
// 获取临时密钥
$policy = [
// 描述策略语法版本。该元素是必填项。目前仅允许值为“2.0”
"version" => "2.0",
// 描述一条或多条权限的详细信息
"statement" => [
[
// 描述声明产生的结果是“允许”还是“显式拒绝”。包括 allow允许和 deny (显式拒绝)两种情况。该元素是必填项
"effect" => "allow",
// 描述允许或拒绝的操作
"action" => [
"cos:PutObject",
"cos:PostObject"
],
// 描述授权的具体数据
"resource" => [
/* qcs-qcloud service 的简称,表示是腾讯云的云资源
service_type:cos产品简称
region:$tecent_cos_param['region'] 描述地域信息
account::uid/$tecent_cos_param['main_app_id'] 描述资源拥有者的主账号信息目前支持两种方式描述资源拥有者uin 和 uid 方式
*/
"qcs::cos:" . $tecent_cos_param['region'] . ":uid/" . $tecent_cos_param['main_app_id'] . ":" . $tecent_cos_param['bucket_name'] . "/*"
],
]
]
];
$result = $credentials_class->getFederationToken('cos', $policy);
$return_data = [
'token' => $result['Credentials']['Token'],
'tem_secret_id' => $result['Credentials']['TmpSecretId'],
'tem_secret_key' => $result['Credentials']['TmpSecretKey'],
'bucket' => $tecent_cos_param['bucket_name'],
'region' => $tecent_cos_param['region'],
'scheme' => Request::scheme(),
// 请求地址
'host' => Request::scheme() . '://' . $tecent_cos_param['bucket_name'] . '.cos.' . $tecent_cos_param['region'] . '.myqcloud.com/',
// 目录
'dir' => $tecent_cos_param['first_directory'] . '/uid' . UID . '/api/' . date('Ymd'),
// 展示地址
'show_url' => $tecent_cos_param['show_url']
];
return sendSuccessMessage($return_data);
}
}