Skip to content

Commit c607267

Browse files
author
guimingwu
committed
add sts ci demo
1 parent 32e3183 commit c607267

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

sample/sts_ci_demo.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* 第一步:获取临时密钥
4+
* 该demo为临时密钥获取sdk的使用示例,具体情参考sdk git地址 https://github.com/tencentyun/qcloud-cos-sts-sdk
5+
* 参考文档 https://cloud.tencent.com/document/product/436/14048
6+
* $config 配置中的 allowCiSource 字段为万象资源配置,为true时授予万象资源权限
7+
* 拿到临时密钥后,可以在cos php sdk中使用 https://github.com/tencentyun/cos-php-sdk-v5
8+
* Array
9+
* (
10+
* [expiredTime] => 1700828878
11+
* [expiration] => 2023-11-24T12:27:58Z
12+
* [credentials] => Array
13+
* (
14+
* [sessionToken] => token
15+
* [tmpSecretId] => secretId
16+
* [tmpSecretKey] => secretKey
17+
* )
18+
*
19+
* [requestId] => 2a521211-b212-xxxx-xxxx-c9976a3966bd
20+
* [startTime] => 1700810878
21+
* )
22+
*/
23+
24+
require_once __DIR__ . '/vendor/autoload.php';
25+
26+
$bucket = 'examplebucket-1250000000';
27+
$secretKey = 'SECRETKEY';
28+
$secretId = 'SECRETID';
29+
$region = "ap-beijing";
30+
31+
$sts = new QCloud\COSSTS\Sts();
32+
$config = array(
33+
'url' => 'https://sts.tencentcloudapi.com/', // url和domain保持一致
34+
'domain' => 'sts.tencentcloudapi.com', // 域名,非必须,默认为 sts.tencentcloudapi.com
35+
'proxy' => '',
36+
'secretId' => $secretId, // 固定密钥,若为明文密钥,请直接以'xxx'形式填入,不要填写到getenv()函数中
37+
'secretKey' => $secretKey, // 固定密钥,若为明文密钥,请直接以'xxx'形式填入,不要填写到getenv()函数中
38+
'bucket' => $bucket, // 换成你的 bucket
39+
'region' => $region, // 换成 bucket 所在园区
40+
'durationSeconds' => 1800*10, // 密钥有效期
41+
'allowPrefix' => array('/*'), // 这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的具体路径,例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
42+
'allowCiSource' => true, // 万象资源配置,授予万象资源权限
43+
'allowActions' => array (
44+
'name/cos:*',
45+
'name/ci:*',
46+
// 具体action按需设置
47+
),
48+
// // 临时密钥生效条件,关于condition的详细设置规则和COS支持的condition类型可以参考 https://cloud.tencent.com/document/product/436/71306
49+
// "condition" => array(
50+
// "ip_equal" => array(
51+
// "qcs:ip" => array(
52+
// "10.217.182.3/24",
53+
// "111.21.33.72/24",
54+
// )
55+
// )
56+
// )
57+
);
58+
59+
60+
try {
61+
// 获取临时密钥,计算签名
62+
$tempKeys = $sts->getTempKeys($config);
63+
print_r($tempKeys);
64+
} catch (Exception $e) {
65+
echo $e;
66+
}
67+
68+
69+
/**
70+
* 第二步:在cos php sdk中使用临时密钥
71+
* 创建临时密钥生成的Client,以文本同步审核为例
72+
*/
73+
// 临时密钥
74+
$tmpSecretId = 'secretId'; // 第一步获取到的 $tempKeys['credentials']['tmpSecretId']
75+
$tmpSecretKey = 'secretKey'; // 第一步获取到的 $tempKeys['credentials']['tmpSecretKey']
76+
$token = 'token'; // 第一步获取到的 $tempKeys['credentials']['sessionToken']
77+
$tokenClient = new Qcloud\Cos\Client(
78+
array(
79+
'region' => $region,
80+
'scheme' => 'https', //协议头部,默认为http
81+
'credentials'=> array(
82+
'secretId' => $tmpSecretId ,
83+
'secretKey' => $tmpSecretKey,
84+
'token' => $token,
85+
)
86+
)
87+
);
88+
89+
try {
90+
$content = '敏感词';
91+
$result = $tokenClient->detectText(array(
92+
'Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
93+
'Input' => array(
94+
'Content' => base64_encode($content), // 文本需base64_encode
95+
),
96+
));
97+
// 请求成功
98+
print_r($result);
99+
} catch (\Exception $e) {
100+
// 请求失败
101+
echo($e);
102+
}

0 commit comments

Comments
 (0)