1. 首页 > 云服务器

阿里云CDN刷新链接接口实践

今天更改了一个线上的css文件,由于项目部署了cdn,需要对缓存进行清理。有两种方式;

一、清除cdn缓存

二、更改页面引入文件地址,如加入版本号

第一种方式需要登录阿里云,然后进行缓存清理操作,比较麻烦;

第二种方式如果是涉及的页面数量过多,在更改起来也是非常麻烦的。

我们使用的阿里云的cdn产品,记得以往的产品都有api接口,想通过阿里云cdn接口模式对链接进行缓存清除操作。

下面是php实例:

一、安装SDK

使用composer进行安装,命令:

composer require alibabacloud/client

二、获取阿里云参数

1、accessKeyId

2、accessSecret

建议使用子密钥

  1. <?php?
  2. /**?
  3. ?*?Created?by?PhpStorm.?
  4. ?*?User:?Administrator?
  5. ?*?Date:?2019/10/29?0029?
  6. ?*?Time:?10:10?
  7. ?*/?
  8. ??
  9. namespace?App\index\controller;?
  10. ??
  11. use?AlibabaCloud\Client\AlibabaCloud;?
  12. use?AlibabaCloud\Client\Exception\ClientException;?
  13. use?AlibabaCloud\Client\Exception\ServerException;?
  14. use?think\Controller;?
  15. use?think\Request;?
  16. ??
  17. class?Cdn?extends?Controller?
  18. {?
  19. ????protected?$accessKeyId;?
  20. ????protected?$accessSecret;?
  21. ??
  22. ????public?function?__construct(Request?$request?=?null)?
  23. ????{?
  24. ????????parent::__construct($request);?
  25. ????????$this->accessKeyId??=?'123456789';?
  26. ????????$this->accessSecret?=?'123456789';?
  27. ????}?
  28. ??
  29. ????public?function?index()?
  30. ????{?
  31. ????????$url?=?'http://www.demo.com/css/index.css';?
  32. ????????AlibabaCloud::accessKeyClient($this->accessKeyId,?$this->accessSecret)?
  33. ????????????->regionId('cn-hangzhou')?
  34. ????????????->asDefaultClient();?
  35. ????????try?{?
  36. ????????????$result?=?AlibabaCloud::rpc()?
  37. ????????????????->product('Cdn')?
  38. ????????????????//?->scheme('https')?//?https?|?http?
  39. ????????????????->version('2018-05-10')?
  40. ????????????????->action('RefreshObjectCaches')?
  41. ????????????????->method('POST')?
  42. ????????????????->host('cdn.aliyuncs.com')?
  43. ????????????????->options([?
  44. ????????????????????'query'?=>?[?
  45. ????????????????????????'RegionId'???=>?"cn-hangzhou",?
  46. ????????????????????????'ObjectPath'?=>?$url,?
  47. ????????????????????],?
  48. ????????????????])?
  49. ????????????????->request();?
  50. ????????????$res????=?$result->toArray();?
  51. ????????????if(isset($res['RefreshTaskId']))?{?
  52. ????????????????echo?'刷新成功';?
  53. ????????????}?else?{?
  54. ????????????????echo?'刷新失败';?
  55. ????????????}?
  56. ????????}?
  57. ????????catch(ClientException?$e)?{?
  58. ????????????echo?$e->getErrorMessage()?.?PHP_EOL;?
  59. ????????}?
  60. ????????catch(ServerException?$e)?{?
  61. ????????????echo?$e->getErrorMessage()?.?PHP_EOL;?
  62. ????????}?
  63. ??
  64. ????}?
  65. }?

建议在提交刷新后,2分钟后查看效果,注意要清除浏览器缓存哦!

本文由主机测评网发布,不代表主机测评网立场,转载联系作者并注明出处:/yunfuwuqi/253.html

联系我们

在线咨询:点击这里给我发消息

Q Q:2220678578

http://www.vxiaotou.com