七牛CDN刷新,预取,获取流量,带宽功能参考实现
该参考实现基于七牛的java sdk,可以从 https//github.com/qiniu/java-sdk 下载。
代码如下:
package com.qiniulab.cdn;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import com.qiniu.common.QiniuException;import com.qiniu.http.Client;import com.qiniu.http.Response;import com.qiniu.util.Auth;import com.qiniu.util.Json;import com.qiniu.util.StringMap;/ * Fusion Cdn 提供的功能 * * * 1. 刷新链接和目录 * 1. 预取资源 * 1. 获取流量数据 * 1. 获取带宽数据 * */public class FusionCdn { private Auth auth; public FusionCdn(String accessKey, String secretKey) { this.auth = Auth.create(accessKey, secretKey); } / * 刷新目录或者链接列表,注意目录必须以 / 结尾 * * @param urls * 待刷新的链接列表 * @param dirs * 待刷新的目录列表 * @throws QiniuException */ public CdnRefreshResult refresh(String[] urls, String[] dirs) throws QiniuException { CdnRefreshResult refreshResult = null; String refreshAPI = "http://fusion.qiniuapi.com/refresh"; Client client = new Client(); StringMap map = new StringMap(); map.put("urls", urls); map.put("dirs", dirs); String postBody = Json.encode(map); String accessToken = String.format("QBox %s", this.auth.signRequest(refreshAPI, null, null)); StringMap headers = new StringMap(); headers.put("Authorization", accessToken); try { Response resp = client.post(refreshAPI, postBody.getBytes("UTF-8"), headers, Client.JsonMime); refreshResult = resp.jsonToObject(CdnRefreshResult.class); } catch (UnsupportedEncodingException e) { } return refreshResult; } / * 根据资源访问外链进行预取操作,不支持预取目录 * * @param urls * 待预取的链接列表 * @throws QiniuException */ public CdnPrefetchResult prefetch(String[] urls) throws QiniuException { CdnPrefetchResult prefetchResult = null; String refreshAPI = "http://fusion.qiniuapi.com/prefetch"; Client client = new Client(); StringMap map = new StringMap(); map.put("urls", urls); String postBody = Json.encode(map); String accessToken = String.format("QBox %s", this.auth.signRequest(refreshAPI, null, null)); StringMap headers = new StringMap(); headers.put("Authorization", accessToken); try { Response resp = client.post(refreshAPI, postBody.getBytes("UTF-8"), headers, Client.JsonMime); prefetchResult = resp.jsonToObject(CdnPrefetchResult.class); } catch (UnsupportedEncodingException e) { } return prefetchResult; } / * @param domain * 需要获取带宽的域名 * @param startTime * 起始时间 * @param endTime * 结束时间 */ public DomainBandwidthResult getDomainBandwidth(String domain, String startTime, String endTime) throws QiniuException { DomainBandwidthResult bandwidthResult; String reqUrl = null; try { reqUrl = String.format("http://fusion.qiniuapi.com/domain/bandwidth?domain=%s&startTime=%s&endTime=%s", URLEncoder.encode(domain, "utf-8"), URLEncoder.encode(startTime, "utf-8"), URLEncoder.encode(endTime, "utf-8")); } catch (UnsupportedEncodingException e1) { } Client client = new Client(); StringMap map = new StringMap(); map.put("Content-Type", Client.FormMime); String accessToken = String.format("QBox %s", this.auth.signRequest(reqUrl, null, null)); StringMap headers = new StringMap(); headers.put("Authorization", accessToken); Response resp = client.get(reqUrl, headers); bandwidthResult = resp.jsonToObject(DomainBandwidthResult.class); return bandwidthResult; } / * @param domain * 需要获取流量的域名 * @param startTime * 起始时间 * @param endTime * 结束时间 */ public DomainFlowResult getDomainFlow(String domain, String startTime, String endTime) throws QiniuException { DomainFlowResult flowResult; String reqUrl = null; try { reqUrl = String.format("http://fusion.qiniuapi.com/domain/traffic?domain=%s&startTime=%s&endTime=%s", URLEncoder.encode(domain, "utf-8"), URLEncoder.encode(startTime, "utf-8"), URLEncoder.encode(endTime, "utf-8")); } catch (UnsupportedEncodingException e1) { } Client client = new Client(); StringMap map = new StringMap(); map.put("Content-Type", Client.FormMime); String accessToken = String.format("QBox %s", this.auth.signRequest(reqUrl, null, null)); StringMap headers = new StringMap(); headers.put("Authorization", accessToken); Response resp = client.get(reqUrl, headers); flowResult = resp.jsonToObject(DomainFlowResult.class); return flowResult; }}
实例调用:
刷新目录或链接
// ak,sk从 https://portal.qiniu.com/user/key 获取String ak = "";String sk = "";FusionCdn cdn = new FusionCdn(ak, sk);// 刷新例子String[] urlsToRefresh = new String[] { "http://if-pbl.qiniudn.com/golang.png", "http://if-pbl.qiniudn.com/test/golang.png" };CdnRefreshResult refreshResult;try { refreshResult = cdn.refresh(urlsToRefresh, null); System.out.println(refreshResult.getCode() + "," + refreshResult.getError());} catch (QiniuException e) { if (e.response != null) { System.err.println(e.response.toString()); }}
资源链接预取
//ak,sk从 https://portal.qiniu.com/user/key 获取String ak = "";String sk = "";FusionCdn cdn = new FusionCdn(ak, sk);// 预取例子String[] urlsToPrefetch = new String[] { "http://if-pbl.qiniudn.com/golang.png", "http://if-pbl.qiniudn.com/test/golang.png" };CdnPrefetchResult prefetchResult;try { prefetchResult = cdn.prefetch(urlsToPrefetch); System.out.println(prefetchResult.getCode() + "," + prefetchResult.getError());} catch (QiniuException e) { if (e.response != null) { System.err.println(e.response.toString()); }}
获取域名带宽数据
// ak,sk从 https://portal.qiniu.com/user/key 获取String ak = "";String sk = "";String domain = "sc.example.com";String startTime = "2016-06-30 18:00:00";String endTime = "2016-06-30 20:00:00";FusionCdn cdn = new FusionCdn(ak, sk);try { DomainBandwidthResult bandwidthResult = cdn.getDomainBandwidth(domain, startTime, endTime); System.out.println(bandwidthResult.getCode());} catch (QiniuException e) { if (e.response != null) { System.err.println(e.response.toString()); }}
获取域名流量数据
// ak,sk从 https://portal.qiniu.com/user/key 获取String ak = "";String sk = "";String domain = "sc.example.com";String startTime = "2016-06-30 18:00:00";String endTime = "2016-06-30 20:00:00";FusionCdn cdn = new FusionCdn(ak, sk);try { DomainFlowResult flowResult = cdn.getDomainFlow(domain, startTime, endTime); System.out.println(flowResult.getCode());} catch (QiniuException e) { if (e.response != null) { System.err.println(e.response.toString()); }}
关键字:七牛云存储
版权声明
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!