新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
阿里云CDN刷新鏈接接口實踐
今天更改了一個線上的css文件,由于項目部署了cdn,需要對緩存進行清理。有兩種方式;
一、清除cdn緩存
二、更改頁面引入文件地址,如加入版本號
第一種方式需要登錄阿里云,然后進行緩存清理操作,比較麻煩;
第二種方式如果是涉及的頁面數(shù)量過多,在更改起來也是非常麻煩的。
我們使用的阿里云的cdn產(chǎn)品,記得以往的產(chǎn)品都有api接口,想通過阿里云cdn接口模式對鏈接進行緩存清除操作。
下面是php實例:成都服務(wù)器托管
一、安裝SDK
使用composer進行安裝,命令:成都服務(wù)器托管
composer require alibabacloud/client
二、獲取阿里云參數(shù)
1、accessKeyId
2、accessSecret
建議使用子密鑰
- /**
- *CreatedbyPhpStorm.
- *User:Administrator
- *Date:2019/10/290029
- *Time:10:10
- */
- namespaceApp\index\controller;
- useAlibabaCloud\Client\AlibabaCloud;
- useAlibabaCloud\Client\Exception\ClientException;
- useAlibabaCloud\Client\Exception\ServerException;
- usethink\Controller;
- usethink\Request;
- classCdnextendsController
- {
- protected$accessKeyId;
- protected$accessSecret;
- publicfunction__construct(Request$request=null)
- {
- parent::__construct($request);
- $this->accessKeyId='123456789';
- $this->accessSecret='123456789';
- }
- publicfunctionindex()
- {
- $url='http://www.demo.com/css/index.css';
- AlibabaCloud::accessKeyClient($this->accessKeyId,$this->accessSecret)
- ->regionId('cn-hangzhou')
- ->asDefaultClient();
- try{
- $result=AlibabaCloud::rpc()
- ->product('Cdn')
- //->scheme('https')//https|http
- ->version('2018-05-10')
- ->action('RefreshObjectCaches')
- ->method('POST')
- ->host('cdn.aliyuncs.com')
- ->options([
- 'query'=>[
- 'RegionId'=>"cn-hangzhou",
- 'ObjectPath'=>$url,
- ],
- ])
- ->request();
- $res=$result->toArray();
- if(isset($res['RefreshTaskId'])){
- echo'刷新成功';
- }else{
- echo'刷新失敗';
- }
- }
- catch(ClientException$e){
- echo$e->getErrorMessage().PHP_EOL;
- }
- catch(ServerException$e){
- echo$e->getErrorMessage().PHP_EOL;
- }
- }
- }
建議在提交刷新后,2分鐘后查看效果,注意要清除瀏覽器緩存哦!
網(wǎng)頁標題:阿里云CDN刷新鏈接接口實踐
網(wǎng)頁URL:http://www.ef60e0e.cn/article/gjip.html