PHP code example of zeroone2005 / qcloud-sts-sdk
1. Go to this page and download the library: Download zeroone2005/qcloud-sts-sdk library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zeroone2005 / qcloud-sts-sdk example snippets
use Qcloud\STS;
//方法一
// 配置参数
$config = array(
'url' => 'https://sts.tencentcloudapi.com/',
'domain' => 'sts.tencentcloudapi.com',
//'proxy' => null, //设置网络请求代理,若不需要设置,则为null
'secretId' => 'AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // 云 API 密钥 secretId
'secretKey' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // 云 API 密钥 secretKey
'bucket' => 'example-1250000000', // 换成你的 bucket
'region' => 'ap-guangzhou', // 换成 bucket 所在地区
'durationSeconds' => 1800, // 密钥有效期
'allowPrefix' => '*', // 设置可操作的资源路径前缀,根据实际情况进行设置,如授予可操作所有的资源:则为 *; 如授予操作某个路径a下的所有资源,则为 a/*;如授予只能操作某个特定路径的文件 a/test.jpg, 则为 a/test.jpg
// 密钥的权限列表。简单上传和分片需要以下的权限,其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
'allowActions' => array (
// 简单上传
'name/cos:PutObject',
// 表单上传
'name/cos:PostObject',
// 分片上传: 初始化分片
'name/cos:InitiateMultipartUpload',
// 分片上传: 查询 bucket 中未完成分片上传的UploadId
"name/cos:ListMultipartUploads",
// 分片上传: 查询已上传的分片
"name/cos:ListParts",
// 分片上传: 上传分片块
"name/cos:UploadPart",
// 分片上传: 完成分片上传
"name/cos:CompleteMultipartUpload"
)
);
//创建 sts
$sts = new STS();
// 获取临时密钥,计算签名
$tempKeys = $sts->getTempKeys($config);
echo json_encode($tempKeys);
//方法二
//设置策略 policy,可通过 STS 的 getPolicy($scopes)获取
$actions=array('name/cos:PutObject'); // 简单上传
$resources = array("qcs::cos:ap-guangzhou:uid/12500000:example-1250000000/*"); // 设置可操作的资源路径前缀,根据实际情况进行设置
$statements = array(array(
'action' => $actions,
'effect' => 'allow',
'resource' => $resources
));
$policy = array(
'version'=> '2.0',
'statement' => $statements
);
// 配置参数
$config = array(
'url' => 'https://sts.tencentcloudapi.com/',
'domain' => 'sts.tencentcloudapi.com',
//'proxy' => null, //设置网络请求代理,若不需要设置,则为null
'secretId' => 'AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // 云 API 密钥 secretId
'secretKey' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // 云 API 密钥 secretKey
'policy' => $policy //策略
)
);
//创建 sts
$sts = new STS();
// 获取临时密钥,计算签名
$tempKeys = $sts->getTempKeys($config);
echo json_encode($tempKeys);
{
"credentials": {
"tmpSecretId": "AKIDEPMQB_Q9Jt2fJxXyIekOzKZzx-sdGQgBga4TzsUdTWL9xlvsjInOHhCYFqfoKOY4",
"tmpSecretKey": "W/3Lbl1YEW02mCoawIesl5kNehSskrSbp1cT1tgW70g=",
"sessionToken": "c6xnSYAxyFbX8Y50627y9AA79u6Qfucw6924760b61588b79fea4c277b01ba157UVdr_10Y30bdpYtO8CXedYZe3KKZ_DyzaPiSFfNAcbr2MTfAgwJe-dhYhfyLMkeCqWyTNF-rOdOb0rp4Gto7p4yQAKuIPhQhuDd77gcAyGakC2WXHVd6ZuVaYIXBizZxqIHAf4lPiLHa6SZejSQfa_p5Ip2U1cAdkEionKbrX97xTKTcA_5Pu525CFSzHZIQibc2uNMZ-IRdQp12MaXZB6bxM6nB4xXH45mDIlbIGjaAsrtRJJ3csmf82uBKaJrYQoguAjBepMH91WcH87LlW9Ya3emNfVX7NMRRf64riYd_vomGF0TLgan9smEKAOdtaL94IkLvVJdhLqpvjBjp_4JCdqwlFAixaTzGJHdJzpGWOh0mQ6jDegAWgRYTrJvc5caYTz7Vphl8XoX5wHKKESUn_vqyTAid32t0vNYE034FIelxYT6VXuetYD_mvPfbHVDIXaFt7e_O8hRLkFwrdAIVaUml1mRPvccv2qOWSXs"
},
"expiration": "2019-08-07T08:54:35Z",
"startTime": 1565166275,
"expiredTime": 1565168075
}
use Qcloud\STS;
$scopes = array();
array_push($scopes,new Scope("name/cos:PutObject", "example-1250000000", "ap-guangzhou", "/1.txt"));
array_push($scopes, new Scope("name/cos:GetObject", "example-1250000000", "ap-guangzhou", "/dir/*"));
//创建 sts
$sts = new STS();
//获取policy
$policy= $sts->getPolicy($scopes);
echo str_replace('\\/', '/', json_encode($policy));
{
"version":"2.0",
"statement":[
{
"action":["name/cos:PutObject"],
"effect":"allow",
"resource":["qcs::cos:ap-guangzhou:uid/12500000:example-1250000000/1.txt"]
},
{
"action":["name/cos:GetObject" ],
"effect":"allow",
"resource":["qcs::cos:ap-guangzhou:uid/12500000:example-1250000000/dir/*" ]
}
]
}