sts_ci_demo.php 3.7 KB

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