CDN相关功能
在使用CDN相关功能之前,需要构建CdnManager
对象:
mac := qbox.NewMac(accessKey, secretKey)
cdnManager := cdn.NewCdnManager(mac)
文件刷新
//刷新链接,单次请求链接不可以超过100个,如果超过,请分批发送请求
urlsToRefresh := []string{
"http://if-pbl.qiniudn.com/qiniu.png",
"http://if-pbl.qiniudn.com/github.png",
}
ret, err := cdnManager.RefreshUrls(urlsToRefresh)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(ret.Code)
fmt.Println(ret.RequestID)
目录刷新
// 刷新目录,刷新目录需要联系七牛技术支持开通权限
// 单次请求链接不可以超过10个,如果超过,请分批发送请求
dirsToRefresh := []string{
"http://if-pbl.qiniudn.com/images/",
"http://if-pbl.qiniudn.com/static/",
}
ret, err := cdnManager.RefreshDirs(dirsToRefresh)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(ret.Code)
fmt.Println(ret.RequestID)
fmt.Println(ret.Error)
文件预取
// 预取链接,单次请求链接不可以超过100个,如果超过,请分批发送请求
urlsToPrefetch := []string{
"http://if-pbl.qiniudn.com/qiniu.png",
"http://if-pbl.qiniudn.com/github.png",
}
ret, err := cdnManager.PrefetchUrls(urlsToPrefetch)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(ret.Code)
fmt.Println(ret.RequestID)
获取域名流量
domains := []string{
"if-pbl.qiniudn.com",
"qdisk.qiniudn.com",
}
startDate := "2017-07-30"
endDate := "2017-07-31"
granularity := "day"
data, err := cdnManager.GetFluxData(startDate, endDate, granularity, domains)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%v\n", data)
获取域名带宽
domains := []string{
"if-pbl.qiniudn.com",
"qdisk.qiniudn.com",
}
startDate := "2017-07-30"
endDate := "2017-07-31"
granularity := "day"
data, err := cdnManager.GetBandwidthData(startDate, endDate, granularity, domains)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%v\n", data)
获取日志下载链接
domains := []string{
"if-pbl.qiniudn.com",
"qdisk.qiniudn.com",
}
day := "2017-07-30"
ret, err := cdnManager.GetCdnLogList(day, domains)
if err != nil {
fmt.Println(err)
return
}
domainLogs := ret.Data
for domain, logs := range domainLogs {
fmt.Println(domain)
for _, item := range logs {
fmt.Println(item.Name, item.URL, item.Size, item.ModifiedTime)
}
}
构建时间戳防盗链访问链接
具体算法可以参考:时间戳防盗链
urlStr := "http://img.abc.com/test.jpg"
cryptKey := "abc123"
deadline := time.Now().Add(time.Second * 3600).Unix()
accessUrl, err := cdn.CreateTimestampAntileechURL(urlStr, cryptKey, deadline)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(accessUrl)