七牛CDN革新,预取,猎取流量,带宽功用参考完成

本文中的代码文件在:https://github.com/qiniudemo/qiniu-lab-java/tree/master/src/com/qiniulab/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 供应的功用
 * 
 * <ol>
 * <li>革新链接和目次</li>
 * <li>预取资本</li>
 * <li>猎取流量数据</li>
 * <li>猎取带宽数据</li>
 * </ol>
 */

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());
    }
}    
    原文作者:jemygraw
    原文地址: https://segmentfault.com/a/1190000005844003
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞