参考文档:https://cloud.tencent.com/document/product/598/13896
参考github项目:https://github.com/tencentyun/qcloud-cos-sts-sdk/tree/master/php

下面是参考以上文档写的方法

/**
    * 获取腾讯云存储联合身份临时访问凭证
    * 用户app端使用腾讯云存储进行文件上传前进行身份验证,验证通过才可上传文件
    * 参考文档:https://cloud.tencent.com/document/product/598/13896
    * 参考github项目:https://github.com/tencentyun/qcloud-cos-sts-sdk/tree/master/php
    * @return int code 操作码,0表示成功
    * @return string msg 提示信息
    * @return array info 返回信息
    * @return string info[0].sessionToken 返回验证token
    * @return string info[0].tmpSecretId 返回临时secretid
    * @return string info[0].tmpSecretKey 返回临时secretkey
    * @return string info[0].requestId 返回requestId
    * @return string info[0].expiredTime 返回expiredTime 有效日期截止时间
    */

public function getTxCosFederationToken(){

        $rs=array('code'=>0,"msg"=>"","info"=>array());

        require_once(API_ROOT.'/../sdk/tencentSts/sts/sts.php');  //根据自己的文件引入路径自行修改
        $sts = new STS();
        

        $config = array(
            'url' => 'https://sts.tencentcloudapi.com/',
            'domain' => 'sts.tencentcloudapi.com',
            'proxy' => '',
            'secretId' => '', // 腾讯云存储secretid密钥
            'secretKey' => '', // 腾讯云存储secretkey
            'bucket' => '', // bucket-appid,如:ceshi-1255549201
            'region' => '', // 换成 bucket 所在地区 如ap-shanghai
            'durationSeconds' => 1800, // 密钥有效期
            'allowPrefix' => '*', // 这里改成允许的路径前缀,可以根据自己网站的用户登录状态判断允许上传的具体路径,例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
            // 密钥的权限列表。简单上传和分片需要以下的权限,其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
            'allowActions' => array (
                // 简单上传
                'name/cos:PutObject',
                'name/cos:PostObject',
                // 分片上传
                'name/cos:InitiateMultipartUpload',
                'name/cos:ListMultipartUploads',
                'name/cos:ListParts',
                'name/cos:UploadPart',
                'name/cos:CompleteMultipartUpload'
            )
        );

        // 获取临时密钥,计算签名
        $tempKeys = $sts->getTempKeys($config);

        $info['sessionToken']=$tempKeys['credentials']['sessionToken'];
        $info['tmpSecretId']=$tempKeys['credentials']['tmpSecretId'];
        $info['tmpSecretKey']=$tempKeys['credentials']['tmpSecretKey'];
        $info['requestId']=$tempKeys['requestId'];
        $info['expiredTime']=(string)$tempKeys['expiredTime'];

        $rs['info'][0]=$info;

        return $rs;
    }

 

最后返回的内容结构如下:

 

 

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐