CDN相关功能

在使用CDN相关功能之前,需要构建CdnManager对象:

  1. mac := qbox.NewMac(accessKey, secretKey)
  2. cdnManager := cdn.NewCdnManager(mac)

文件刷新

  1. //刷新链接,单次请求链接不可以超过100个,如果超过,请分批发送请求
  2. urlsToRefresh := []string{
  3. "http://if-pbl.qiniudn.com/qiniu.png",
  4. "http://if-pbl.qiniudn.com/github.png",
  5. }
  6. ret, err := cdnManager.RefreshUrls(urlsToRefresh)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(ret.Code)
  12. fmt.Println(ret.RequestID)

目录刷新

  1. // 刷新目录,刷新目录需要联系七牛技术支持开通权限
  2. // 单次请求链接不可以超过10个,如果超过,请分批发送请求
  3. dirsToRefresh := []string{
  4. "http://if-pbl.qiniudn.com/images/",
  5. "http://if-pbl.qiniudn.com/static/",
  6. }
  7. ret, err := cdnManager.RefreshDirs(dirsToRefresh)
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(ret.Code)
  13. fmt.Println(ret.RequestID)
  14. fmt.Println(ret.Error)

文件预取

  1. // 预取链接,单次请求链接不可以超过100个,如果超过,请分批发送请求
  2. urlsToPrefetch := []string{
  3. "http://if-pbl.qiniudn.com/qiniu.png",
  4. "http://if-pbl.qiniudn.com/github.png",
  5. }
  6. ret, err := cdnManager.PrefetchUrls(urlsToPrefetch)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. fmt.Println(ret.Code)
  12. fmt.Println(ret.RequestID)

获取域名流量

  1. domains := []string{
  2. "if-pbl.qiniudn.com",
  3. "qdisk.qiniudn.com",
  4. }
  5. startDate := "2017-07-30"
  6. endDate := "2017-07-31"
  7. granularity := "day"
  8. data, err := cdnManager.GetFluxData(startDate, endDate, granularity, domains)
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Printf("%v\n", data)

获取域名带宽

  1. domains := []string{
  2. "if-pbl.qiniudn.com",
  3. "qdisk.qiniudn.com",
  4. }
  5. startDate := "2017-07-30"
  6. endDate := "2017-07-31"
  7. granularity := "day"
  8. data, err := cdnManager.GetBandwidthData(startDate, endDate, granularity, domains)
  9. if err != nil {
  10. fmt.Println(err)
  11. return
  12. }
  13. fmt.Printf("%v\n", data)

获取日志下载链接

  1. domains := []string{
  2. "if-pbl.qiniudn.com",
  3. "qdisk.qiniudn.com",
  4. }
  5. day := "2017-07-30"
  6. ret, err := cdnManager.GetCdnLogList(day, domains)
  7. if err != nil {
  8. fmt.Println(err)
  9. return
  10. }
  11. domainLogs := ret.Data
  12. for domain, logs := range domainLogs {
  13. fmt.Println(domain)
  14. for _, item := range logs {
  15. fmt.Println(item.Name, item.URL, item.Size, item.ModifiedTime)
  16. }
  17. }

构建时间戳防盗链访问链接

具体算法可以参考:时间戳防盗链

  1. urlStr := "http://img.abc.com/test.jpg"
  2. cryptKey := "abc123"
  3. deadline := time.Now().Add(time.Second * 3600).Unix()
  4. accessUrl, err := cdn.CreateTimestampAntileechURL(urlStr, cryptKey, deadline)
  5. if err != nil {
  6. fmt.Println(err)
  7. return
  8. }
  9. fmt.Println(accessUrl)