概要

このガイドでは、簡単なシナリオに従ってGlobal CDNインスタンスを照会しそしてPurgeを遂行します。 詳しい文書は、NAVERクラウドプラットフォームGlobal CDN Purge API referenceをご参照ください。

シナリオ実行

CDN インスタンスとドメイン情報照会

CDN インスタンスドメインリスト照会APIを利用して、cdnInstanceIDとcdn設定情報を確認します。

  • アクション

    • getGlobalCdnInstanceList
  • アクションパラメータ

    • なし
旧バージョン(12月 Deprecated)

OAuth外部ライブラリを利用して、authorizationヘッダにauthparamsを含めて簡単にリクエストできます。
詳しい事項はNcloud OAuth認証ガイドをご参照ください。

signpost-core ライブラリ : http://code.google.com/p/oauth-signpost/wiki/GettingStarted

  • consumerKey : キー管理で見られるaccessKey
  • consumerSecret : キー管理で見られるsecretKey
  • requstUrl : API エンドポイントURL
  • requestMethod : HTTP Request メソッド
public class OldSignClass {
    public static void main(String args[]) throws Exception {
        String consumerKey = $ConsumerAcessKey$;
        String consumerSecret = $ConsumerSecretKey$;
        String requestUrl = "https://api.ncloud.com/cdn/";
        String requestMethod = "GET";

        Map<String, Object> requestParameters = new HashMap<String, Object>();
        requestParameters.put("action", "getGlobalCdnInstanceList");
        requestParameters.put("responseFormatType", "json");
        // requestParameters.put("cdnInstanceNo", $InstanceNo$);
        // InstanceNoはオプションです。
        // 入力すると、該当インスタンスのみ照会し、入力しなければ、すべてのインスタンスのリストを照会します。


        StringBuffer queryString = new StringBuffer();
        int index= 1;
        int requestParamSize = requestParameters.size();
        for (String key : requestParameters.keySet()) {
            if (index == 1) {
                queryString.append("?");
            }
            queryString.append(key);

            if (requestParameters.get(key) != null) {
                queryString.append('=');
                queryString.append(URLEncoder.encode(requestParameters.get(key).toString(), "UTF-8"));
            }
            if (index!= requestParamSize) {
                queryString.append("&");
                index ++;
            }
        }

        OAuthConsumer consumer = new DefaultOAuthConsumer(consumerKey, consumerSecret);
        URL url = new URL(requestUrl + queryString.toString());
        HttpURLConnection con = (HttpURLConnection)url.openConnection();

        con.setRequestMethod(requestMethod);
        con.setConnectTimeout(500000);
        con.setUseCaches(false);
        con.setDefaultUseCaches(false);

        consumer.sign(con);
        con.connect();

        int responseCode = con.getResponseCode();
        System.out.println("ResponseCode : " + responseCode);

        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader resultReader = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
            StringBuffer requestResult = new StringBuffer();
            String readString = null;
            while ((readString = resultReader.readLine()) != null) {
                requestResult.append(readString);
            }
            System.out.println(requestResult.toString());
        }
    }
}

メインメソッドを実行すると、その結果が得られます。

{"getGlobalCdnInstanceListResponse": {  "requestId": "ee21d017-b093-453c-a886-d359aaafc05d",  "returnCode": "0",  "returnMessage": "success",  "totalRows": 1,  "globalCdnInstanceList": [    {      "cdnInstanceNo": "931582",      "cdnInstanceStatusName": "running",      "createDate": "2018-09-10T18:14:56+0900",      "lastModifiedDate": "2018-09-10T19:24:11+0900",      "cdnInstanceDescription": "",      "serviceName": "test-gcdn-1",      "isAvailablePartialDomainPurge": false,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "globalCdnRule": {        "protocolTypeCode": "HTTP",        "serviceDomainTypeCode": "DEFAULT",        "originUrl": "bucket-1.kr.objectstorage.ncloud.com",        "originPath": "",        "originHttpPort": 80,        "originHttpsPort": 0,        "forwardHostHeaderTypeCode": "ORIGIN_HOSTNAME",        "forwardHostHeader": "",        "cacheKeyHostNameTypeCode": "REQUEST_HOST_HEADER",        "isGzipCompressionUse": true,        "cachingOptionTypeCode": "CACHE_CONTROL_AND_EXPIRES",        "isErrorContentsResponseUse": false,        "cachingTtlTime": 604800,        "isQueryStringIgnoreUse": true,        "isRemoveVaryHeaderUse": true,        "isLargeFileOptimizationUse": false,        "gzipResponseTypeCode": "ORIGIN_RESPONSE",        "isReferrerDomainUse": false,        "referrerDomainList": [],        "isReferrerDomainRestrictUse": false,        "isSecureTokenUse": false,        "secureTokenPassword": "",        "isReissueSecureTokenPassword": false,        "isAccessLogUse": false,        "accessLogFileStorageContainerName": ""      }    }  ]}}
新バージョン

新しいAPI は、認証関連ヘッダを追加する過程を経てAPI を呼び出すことになります。 認証関連Signatureを作る方式はv1、v2二つの方式があります。

v1 Signature

    public static String makeSignatureV1(String url, String apiKey, String accessKey, String secretKey, String timestamp, String method) throws Exception{
        String space = " ";
        String newLine = "\n";

        String message = new StringBuilder()
                .append(method)
                .append(space)
                .append(url)
                .append(newLine)
                .append(timestamp)
                .append(newLine)
                .append(apiKey)
                .append(newLine)
                .append(accessKey)
                .toString();

        SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes("UTF-8"), "HmacSHA256");
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(signingKey);

        byte[] rawHmac = mac.doFinal(message.getBytes("UTF-8"));
        String encodeBase64String = Base64.encodeBase64String(rawHmac);

        return encodeBase64String;

    }

v2 Signature

    public static String makeSignatureV2(String url, String accessKey, String secretKey, String timestamp, String method) throws Exception{
        String space = " ";
        String newLine = "\n";

        String message = new StringBuilder()
                .append(method)
                .append(space)
                .append(url)
                .append(newLine)
                .append(timestamp)
                .append(newLine)
                .append(accessKey)
                .toString();

        SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes("UTF-8"), "HmacSHA256");
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(signingKey);

        byte[] rawHmac = mac.doFinal(message.getBytes("UTF-8"));
        String encodeBase64String = Base64.encodeBase64String(rawHmac);

        return encodeBase64String;

    }
  • url : Action 実行 URL (Query Stringを含む)
  • apiKey : API GateWay Access Key
  • accessKey : ユーザー Access Key
  • secretKey : ユーザー Secret Key
  • timestamp : Epoch時間 (Milli秒)
  • method : HTTP Request メソッド

詳しい事項はNcloud APIs共通ガイドをご参照ください。

  • 該当コードはJava 8コードで作成されました。 java.time.Instantオブジェクトがサポートされない場合、TIMESTAMP値をミリ(mili)秒単位まで生成する必要があります。

Java(v1)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class MainClass {
    private final static String API_END_POINT = "https://ncloud.apigw.ntruss.com";
    private final static String getCdnPlusPurgeHistoryList = "/cdn/v1/getGlobalCdnInstanceList";
    private final static String METHODS = "GET";

    private final static String API_GW_KEY = $ConsumerAPIGateWayKey$;
    private final static String ACCESS_KEY = $ConsumerAccessKey$;
    private final static String SECRET_KEY = $ConsumerSecretKey$;
    private final static String TIMESTAMP = Long.toString(Instant.now().toEpochMilli());


    public static void main(String args[]) throws Exception{

        Map<String, Object> requestParameters = new HashMap<String, Object>();
        requestParameters.put("responseFormatType", "json");


        StringBuffer queryString = new StringBuffer();
        int index= 1;
        int requestParamSize = requestParameters.size();
        for (String key : requestParameters.keySet()) {
            if (index == 1) {
                queryString.append("?");
            }
            queryString.append(key);

            if (requestParameters.get(key) != null) {
                queryString.append('=');
                queryString.append(URLEncoder.encode(requestParameters.get(key).toString(), "UTF-8"));
            }
            if (index!= requestParamSize) {
                queryString.append("&");
                index ++;
            }
        }

        String openURL = API_END_POINT + getCdnPlusPurgeHistoryList + queryString;
        URL obj = new URL(openURL);

        HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("x-ncp-apigw-timestamp", TIMESTAMP);
        conn.setRequestProperty("x-ncp-apigw-api-key", API_GW_KEY);
        conn.setRequestProperty("x-ncp-iam-access-key", ACCESS_KEY);
        conn.setRequestProperty("x-ncp-apigw-signature-v1", makeSignatureV1(getCdnPlusPurgeHistoryList + queryString, API_GW_KEY, ACCESS_KEY, SECRET_KEY, TIMESTAMP, METHODS));
        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);

        int responseCode = conn.getResponseCode();
        System.out.println("ResponseCode : " + responseCode);

        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader resultReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            StringBuffer requestResult = new StringBuffer();
            String readString = null;
            while ((readString = resultReader.readLine()) != null) {
                requestResult.append(readString);
            }
            System.out.println(requestResult.toString());
        }

    }
}

メインメソッドを実行すると、その結果が得られます。

{"getGlobalCdnInstanceListResponse": {  "requestId": "0a9f6108-cf42-425f-99d7-b7179d5d1b7c",  "returnCode": "0",  "returnMessage": "success",  "totalRows": 1,  "globalCdnInstanceList": [    {      "cdnInstanceNo": "931582",      "cdnInstanceStatusName": "running",      "createDate": "2018-09-10T18:14:56+0900",      "lastModifiedDate": "2018-09-10T19:24:11+0900",      "cdnInstanceDescription": "",      "serviceName": "test-gcdn-1",      "isAvailablePartialDomainPurge": false,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "globalCdnRule": {        "protocolTypeCode": "HTTP",        "serviceDomainTypeCode": "DEFAULT",        "originUrl": "bucket-1.kr.objectstorage.ncloud.com",        "originPath": "",        "originHttpPort": 80,        "originHttpsPort": 0,        "forwardHostHeaderTypeCode": "ORIGIN_HOSTNAME",        "forwardHostHeader": "",        "cacheKeyHostNameTypeCode": "REQUEST_HOST_HEADER",        "isGzipCompressionUse": true,        "cachingOptionTypeCode": "CACHE_CONTROL_AND_EXPIRES",        "isErrorContentsResponseUse": false,        "cachingTtlTime": 604800,        "isQueryStringIgnoreUse": true,        "isRemoveVaryHeaderUse": true,        "isLargeFileOptimizationUse": false,        "gzipResponseTypeCode": "ORIGIN_RESPONSE",        "isReferrerDomainUse": false,        "referrerDomainList": [],        "isReferrerDomainRestrictUse": false,        "isSecureTokenUse": false,        "secureTokenPassword": "",        "isReissueSecureTokenPassword": false,        "isAccessLogUse": false,        "accessLogFileStorageContainerName": ""      }    }  ]}}

Java(v2)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class MainClass {
    private final static String API_END_POINT = "https://ncloud.apigw.ntruss.com";
    private final static String getCdnPlusPurgeHistoryList = "/cdn/v1/getGlobalCdnInstanceList";
    private final static String METHODS = "GET";

    private final static String API_GW_KEY = $ConsumerAPIGateWayKey$;
    private final static String ACCESS_KEY = $ConsumerAccessKey$;
    private final static String SECRET_KEY = $ConsumerSecretKey$;
    private final static String TIMESTAMP = Long.toString(Instant.now().toEpochMilli());


    public static void main(String args[]) throws Exception{

        Map<String, Object> requestParameters = new HashMap<String, Object>();
        requestParameters.put("responseFormatType", "json");


        StringBuffer queryString = new StringBuffer();
        int index= 1;
        int requestParamSize = requestParameters.size();
        for (String key : requestParameters.keySet()) {
            if (index == 1) {
                queryString.append("?");
            }
            queryString.append(key);

            if (requestParameters.get(key) != null) {
                queryString.append('=');
                queryString.append(URLEncoder.encode(requestParameters.get(key).toString(), "UTF-8"));
            }
            if (index!= requestParamSize) {
                queryString.append("&");
                index ++;
            }
        }

        String openURL = API_END_POINT + getCdnPlusPurgeHistoryList + queryString;
        URL obj = new URL(openURL);

        HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("x-ncp-apigw-timestamp", TIMESTAMP);
        conn.setRequestProperty("x-ncp-apigw-api-key", API_GW_KEY);
        conn.setRequestProperty("x-ncp-iam-access-key", ACCESS_KEY);
        conn.setRequestProperty("x-ncp-apigw-signature-v2", makeSignatureV2(getCdnPlusPurgeHistoryList + queryString, ACCESS_KEY, SECRET_KEY, TIMESTAMP, METHODS));
        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);

        int responseCode = conn.getResponseCode();
        System.out.println("ResponseCode : " + responseCode);

        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader resultReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            StringBuffer requestResult = new StringBuffer();
            String readString = null;
            while ((readString = resultReader.readLine()) != null) {
                requestResult.append(readString);
            }
            System.out.println(requestResult.toString());
        }

    }
}

メインメソッドを実行すると、その結果が得られます。

{"getGlobalCdnInstanceListResponse": {  "requestId": "27d0ef91-deb7-45ec-9153-6886422a33c8",  "returnCode": "0",  "returnMessage": "success",  "totalRows": 1,  "globalCdnInstanceList": [    {      "cdnInstanceNo": "931582",      "cdnInstanceStatusName": "running",      "createDate": "2018-09-10T18:14:56+0900",      "lastModifiedDate": "2018-09-10T19:24:11+0900",      "cdnInstanceDescription": "",      "serviceName": "test-gcdn-1",      "isAvailablePartialDomainPurge": false,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "globalCdnRule": {        "protocolTypeCode": "HTTP",        "serviceDomainTypeCode": "DEFAULT",        "originUrl": "bucket-1.kr.objectstorage.ncloud.com",        "originPath": "",        "originHttpPort": 80,        "originHttpsPort": 0,        "forwardHostHeaderTypeCode": "ORIGIN_HOSTNAME",        "forwardHostHeader": "",        "cacheKeyHostNameTypeCode": "REQUEST_HOST_HEADER",        "isGzipCompressionUse": true,        "cachingOptionTypeCode": "CACHE_CONTROL_AND_EXPIRES",        "isErrorContentsResponseUse": false,        "cachingTtlTime": 604800,        "isQueryStringIgnoreUse": true,        "isRemoveVaryHeaderUse": true,        "isLargeFileOptimizationUse": false,        "gzipResponseTypeCode": "ORIGIN_RESPONSE",        "isReferrerDomainUse": false,        "referrerDomainList": [],        "isReferrerDomainRestrictUse": false,        "isSecureTokenUse": false,        "secureTokenPassword": "",        "isReissueSecureTokenPassword": false,        "isAccessLogUse": false,        "accessLogFileStorageContainerName": ""      }    }  ]}}

CDN Purge 実行

  • アクション

    • requestGlobalCdnPurge
  • アクションパラメータ

    • cdnInstanceNo : $InstanceNo$ ((Purgeしたいインスタンス番号)
    • isWholeDomain
      1. true : ドメインが1つのインスタンスで、全体ドメイン対象、顧客保有Nのサービスドメイン+Cache Key Hostnameが'ORIGIN_HOSTNAME'に使用=>全体ドメイン対象Purge実行
      2. false : 顧客保有Nのサービスドメイン+Cache Key Hostnameが'REQUEST_HOST_HEADER'に使用=>ドメイン個別Purge実行
    • isWholePurge : false (個別ファイルPurge実行)
    • targetFileList.1 : /sample_img.jpg (Purge対象ファイル経路とファイル名)
旧バージョン (12月Deprecated)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;

public class OldSignClass {
    public static void main(String args[]) throws Exception {
        String consumerKey = $ConsumerAcessKey$;
        String consumerSecret = $ConsumerSecretKey$;
        String requestUrl = "https://api.ncloud.com/cdn/";
        String requestMethod = "GET";

        Map<String, Object> requestParameters = new HashMap<String, Object>();
        requestParameters.put("action", "requestGlobalCdnPurge");
        requestParameters.put("cdnInstanceNo", $InstanceNo$);
        requestParameters.put("responseFormatType", "json");
        requestParameters.put("isWholePurge", false);
        requestParameters.put("isWholeDomain", true);
        requestParameters.put("targetFileList.1", "/sample_img.jpg");

        StringBuffer queryString = new StringBuffer();
        int index= 1;
        int requestParamSize = requestParameters.size();
        for (String key : requestParameters.keySet()) {
            if (index == 1) {
                queryString.append("?");
            }
            queryString.append(key);

            if (requestParameters.get(key) != null) {
                queryString.append('=');
                queryString.append(URLEncoder.encode(requestParameters.get(key).toString(), "UTF-8"));
            }
            if (index!= requestParamSize) {
                queryString.append("&");
                index ++;
            }
        }

        OAuthConsumer consumer = new DefaultOAuthConsumer(consumerKey, consumerSecret);
        URL url = new URL(requestUrl + queryString.toString());
        HttpURLConnection con = (HttpURLConnection)url.openConnection();

        con.setRequestMethod(requestMethod);
        con.setConnectTimeout(500000);
        con.setUseCaches(false);
        con.setDefaultUseCaches(false);

        consumer.sign(con);
        con.connect();

        int responseCode = con.getResponseCode();
        System.out.println("ResponseCode : " + responseCode);

        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader resultReader = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
            StringBuffer requestResult = new StringBuffer();
            String readString = null;
            while ((readString = resultReader.readLine()) != null) {
                requestResult.append(readString);
            }
            System.out.println(requestResult.toString());
        }
    }
}

メインメソッドを実行すると、その結果が得られます。

{"requestGlobalCdnPurgeResponse": {  "requestId": "907cccf5-8921-422e-8e22-c764008385d2",  "returnCode": "0",  "returnMessage": "success",  "totalRows": 1,  "globalCdnPurgeHistoryList": [    {      "cdnInstanceNo": "931582",      "purgeId": "4f483694-d051-11e8-bc6e-3fa9bcb46bdc",      "isWholePurge": false,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [        "/sample_img.jpg"      ],      "estimatedCompletionDate": "2018-10-15T17:07:11+0900",      "isSuccess": true,      "requestDate": "2018-10-15T17:07:06+0900"    }  ]}}
新バージョン

Java(v1)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class MainClass {
    private final static String API_END_POINT = "https://ncloud.apigw.ntruss.com";
    private final static String getCdnPlusPurgeHistoryList = "/cdn/v1/requestGlobalCdnPurge";
    private final static String METHODS = "GET";

    private final static String API_GW_KEY = $ConsumerAPIGateWayKey$;
    private final static String ACCESS_KEY = $ConsumerAccessKey$;
    private final static String SECRET_KEY = $ConsumerSecretKey$;
    private final static String TIMESTAMP = Long.toString(Instant.now().toEpochMilli());


    public static void main(String args[]) throws Exception{

        Map<String, Object> requestParameters = new HashMap<String, Object>();
        requestParameters.put("cdnInstanceNo", $InstanceNo$);
        requestParameters.put("responseFormatType", "json");
        requestParameters.put("isWholePurge", false);
        requestParameters.put("isWholeDomain", true);
        requestParameters.put("targetFileList.1", "/sample_img.jpg");

        StringBuffer queryString = new StringBuffer();
        int index= 1;
        int requestParamSize = requestParameters.size();
        for (String key : requestParameters.keySet()) {
            if (index == 1) {
                queryString.append("?");
            }
            queryString.append(key);

            if (requestParameters.get(key) != null) {
                queryString.append('=');
                queryString.append(URLEncoder.encode(requestParameters.get(key).toString(), "UTF-8"));
            }
            if (index!= requestParamSize) {
                queryString.append("&");
                index ++;
            }
        }

        String openURL = API_END_POINT + getCdnPlusPurgeHistoryList + queryString;
        URL obj = new URL(openURL);

        HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("x-ncp-apigw-timestamp", TIMESTAMP);
        conn.setRequestProperty("x-ncp-apigw-api-key", API_GW_KEY);
        conn.setRequestProperty("x-ncp-iam-access-key", ACCESS_KEY);
        conn.setRequestProperty("x-ncp-apigw-signature-v1", makeSignatureV1(getCdnPlusPurgeHistoryList + queryString, API_GW_KEY, ACCESS_KEY, SECRET_KEY, TIMESTAMP, METHODS));
        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);

        int responseCode = conn.getResponseCode();
        System.out.println("ResponseCode : " + responseCode);

        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader resultReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            StringBuffer requestResult = new StringBuffer();
            String readString = null;
            while ((readString = resultReader.readLine()) != null) {
                requestResult.append(readString);
            }
            System.out.println(requestResult.toString());
        }

    }
}

メインメソッドを実行すると、その結果が得られます。

{"requestGlobalCdnPurgeResponse": {  "requestId": "2d765d97-792a-435e-8f1e-a7c9ae0de604",  "returnCode": "0",  "returnMessage": "success",  "totalRows": 1,  "globalCdnPurgeHistoryList": [    {      "cdnInstanceNo": "931582",      "purgeId": "915d238c-d051-11e8-a06c-995c49e861cf",      "isWholePurge": false,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [        "/sample_img.jpg"      ],      "estimatedCompletionDate": "2018-10-15T17:09:02+0900",      "isSuccess": true,      "requestDate": "2018-10-15T17:08:57+0900"    }  ]}}

Java(v2)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class MainClass {
    private final static String API_END_POINT = "https://ncloud.apigw.ntruss.com";
    private final static String getCdnPlusPurgeHistoryList = "/cdn/v1/requestGlobalCdnPurge";
    private final static String METHODS = "GET";

    private final static String API_GW_KEY = $ConsumerAPIGateWayKey$;
    private final static String ACCESS_KEY = $ConsumerAccessKey$;
    private final static String SECRET_KEY = $ConsumerSecretKey$;
    private final static String TIMESTAMP = Long.toString(Instant.now().toEpochMilli());


    public static void main(String args[]) throws Exception{

        Map<String, Object> requestParameters = new HashMap<String, Object>();
        requestParameters.put("cdnInstanceNo", $InstanceNo$);
        requestParameters.put("responseFormatType", "json");
        requestParameters.put("isWholePurge", false);
        requestParameters.put("isWholeDomain", true);
        requestParameters.put("targetFileList.1", "/sample_img.jpg");

        StringBuffer queryString = new StringBuffer();
        int index= 1;
        int requestParamSize = requestParameters.size();
        for (String key : requestParameters.keySet()) {
            if (index == 1) {
                queryString.append("?");
            }
            queryString.append(key);

            if (requestParameters.get(key) != null) {
                queryString.append('=');
                queryString.append(URLEncoder.encode(requestParameters.get(key).toString(), "UTF-8"));
            }
            if (index!= requestParamSize) {
                queryString.append("&");
                index ++;
            }
        }

        String openURL = API_END_POINT + getCdnPlusPurgeHistoryList + queryString;
        URL obj = new URL(openURL);

        HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("x-ncp-apigw-timestamp", TIMESTAMP);
        conn.setRequestProperty("x-ncp-apigw-api-key", API_GW_KEY);
        conn.setRequestProperty("x-ncp-iam-access-key", ACCESS_KEY);
        conn.setRequestProperty("x-ncp-apigw-signature-v2", makeSignatureV2(getCdnPlusPurgeHistoryList + queryString, ACCESS_KEY, SECRET_KEY, TIMESTAMP, METHODS));
        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);

        int responseCode = conn.getResponseCode();
        System.out.println("ResponseCode : " + responseCode);

        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader resultReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            StringBuffer requestResult = new StringBuffer();
            String readString = null;
            while ((readString = resultReader.readLine()) != null) {
                requestResult.append(readString);
            }
            System.out.println(requestResult.toString());
        }

    }
}

メインメソッドを実行すると、その結果が得られます。

{"requestGlobalCdnPurgeResponse": {  "requestId": "48f1d87d-ef52-4350-b5af-58ee7e7e50f0",  "returnCode": "0",  "returnMessage": "success",  "totalRows": 1,  "globalCdnPurgeHistoryList": [    {      "cdnInstanceNo": "931582",      "purgeId": "ae866ef0-d051-11e8-b3c7-57140a645b34",      "isWholePurge": false,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [        "/sample_img.jpg"      ],      "estimatedCompletionDate": "2018-10-15T17:09:52+0900",      "isSuccess": true,      "requestDate": "2018-10-15T17:09:47+0900"    }  ]}}

CDN Purge照会

Purge にリクエストした結果を確認したり、以前にリクエストしたPurge履歴を照会します。

  • アクション

    • getGlobalCdnPurgeHistoryList
  • アクションパラメータ

    • cdnInstanceNo : $InstanceNo$
旧バージョン(12月Deprecated)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;

public class OldSignClass {
    public static void main(String args[]) throws Exception {
        String consumerKey = $ConsumerAcessKey$;
        String consumerSecret = $ConsumerSecretKey$;
        String requestUrl = "https://api.ncloud.com/cdn/";
        String requestMethod = "GET";

        Map<String, Object> requestParameters = new HashMap<String, Object>();
        requestParameters.put("action", "getGlobalCdnPurgeHistoryList");
        requestParameters.put("cdnInstanceNo", $InstanceNo$);
        requestParameters.put("responseFormatType", "json");

        StringBuffer queryString = new StringBuffer();
        int index= 1;
        int requestParamSize = requestParameters.size();
        for (String key : requestParameters.keySet()) {
            if (index == 1) {
                queryString.append("?");
            }
            queryString.append(key);

            if (requestParameters.get(key) != null) {
                queryString.append('=');
                queryString.append(URLEncoder.encode(requestParameters.get(key).toString(), "UTF-8"));
            }
            if (index!= requestParamSize) {
                queryString.append("&");
                index ++;
            }
        }

        OAuthConsumer consumer = new DefaultOAuthConsumer(consumerKey, consumerSecret);
        URL url = new URL(requestUrl + queryString.toString());
        HttpURLConnection con = (HttpURLConnection)url.openConnection();

        con.setRequestMethod(requestMethod);
        con.setConnectTimeout(500000);
        con.setUseCaches(false);
        con.setDefaultUseCaches(false);

        consumer.sign(con);
        con.connect();

        int responseCode = con.getResponseCode();
        System.out.println("ResponseCode : " + responseCode);

        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader resultReader = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
            StringBuffer requestResult = new StringBuffer();
            String readString = null;
            while ((readString = resultReader.readLine()) != null) {
                requestResult.append(readString);
            }
            System.out.println(requestResult.toString());
        }
    }
}

メインメソッドを実行すると、その結果が得られます。

{"getGlobalCdnPurgeHistoryListResponse": {  "requestId": "2cae4302-a64b-4505-b443-0360e7d67df4",  "returnCode": "0",  "returnMessage": "success",  "totalRows": 5,  "globalCdnPurgeHistoryList": [    {      "cdnInstanceNo": "931582",      "purgeId": "ae866ef0-d051-11e8-b3c7-57140a645b34",      "isWholePurge": false,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [        "/sample_img.jpg"      ],      "estimatedCompletionDate": "2018-10-15T17:09:52+0900",      "isSuccess": true,      "requestDate": "2018-10-15T17:09:47+0900"    },    {      "cdnInstanceNo": "931582",      "purgeId": "915d238c-d051-11e8-a06c-995c49e861cf",      "isWholePurge": false,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [        "/sample_img.jpg"      ],      "estimatedCompletionDate": "2018-10-15T17:09:02+0900",      "isSuccess": true,      "requestDate": "2018-10-15T17:08:57+0900"    },    {      "cdnInstanceNo": "931582",      "purgeId": "4f483694-d051-11e8-bc6e-3fa9bcb46bdc",      "isWholePurge": false,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [        "/sample_img.jpg"      ],      "estimatedCompletionDate": "2018-10-15T17:07:11+0900",      "isSuccess": true,      "requestDate": "2018-10-15T17:07:06+0900"    },    {      "cdnInstanceNo": "931582",      "purgeId": "f3c40168-c867-11e8-9f09-970b5c3327c0",      "isWholePurge": true,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [],      "estimatedCompletionDate": "2018-10-05T15:33:02+0900",      "isSuccess": true,      "requestDate": "2018-10-05T15:29:02+0900"    },    {      "cdnInstanceNo": "931582",      "purgeId": "ab6580ae-c867-11e8-8087-a74bca8a3bb7",      "isWholePurge": true,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [],      "estimatedCompletionDate": "2018-10-05T15:31:01+0900",      "isSuccess": true,      "requestDate": "2018-10-05T15:27:01+0900"    }  ]}}
新バージョン

Java(v1)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class MainClass {
    private final static String API_END_POINT = "https://ncloud.apigw.ntruss.com";
    private final static String getCdnPlusPurgeHistoryList = "/cdn/v1/getGlobalCdnPurgeHistoryList";
    private final static String METHODS = "GET";

    private final static String API_GW_KEY = $ConsumerAPIGateWayKey$;
    private final static String ACCESS_KEY = $ConsumerAccessKey$;
    private final static String SECRET_KEY = $ConsumerSecretKey$;
    private final static String TIMESTAMP = Long.toString(Instant.now().toEpochMilli());


    public static void main(String args[]) throws Exception{

        Map<String, Object> requestParameters = new HashMap<String, Object>();
        requestParameters.put("cdnInstanceNo", $InstanceNo$);
        requestParameters.put("responseFormatType", "json");

        StringBuffer queryString = new StringBuffer();
        int index= 1;
        int requestParamSize = requestParameters.size();
        for (String key : requestParameters.keySet()) {
            if (index == 1) {
                queryString.append("?");
            }
            queryString.append(key);

            if (requestParameters.get(key) != null) {
                queryString.append('=');
                queryString.append(URLEncoder.encode(requestParameters.get(key).toString(), "UTF-8"));
            }
            if (index!= requestParamSize) {
                queryString.append("&");
                index ++;
            }
        }

        String openURL = API_END_POINT + getCdnPlusPurgeHistoryList + queryString;
        URL obj = new URL(openURL);

        HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("x-ncp-apigw-timestamp", TIMESTAMP);
        conn.setRequestProperty("x-ncp-apigw-api-key", API_GW_KEY);
        conn.setRequestProperty("x-ncp-iam-access-key", ACCESS_KEY);
        conn.setRequestProperty("x-ncp-apigw-signature-v1", makeSignatureV1(getCdnPlusPurgeHistoryList + queryString, API_GW_KEY, ACCESS_KEY, SECRET_KEY, TIMESTAMP, METHODS));
        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);

        int responseCode = conn.getResponseCode();
        System.out.println("ResponseCode : " + responseCode);

        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader resultReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            StringBuffer requestResult = new StringBuffer();
            String readString = null;
            while ((readString = resultReader.readLine()) != null) {
                requestResult.append(readString);
            }
            System.out.println(requestResult.toString());
        }

    }
}

メインメソッドを実行すると、その結果が得られます。

{"getGlobalCdnPurgeHistoryListResponse": {  "requestId": "e222ea0e-a7ea-467b-9b9a-357fab05c7ef",  "returnCode": "0",  "returnMessage": "success",  "totalRows": 5,  "globalCdnPurgeHistoryList": [    {      "cdnInstanceNo": "931582",      "purgeId": "ae866ef0-d051-11e8-b3c7-57140a645b34",      "isWholePurge": false,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [        "/sample_img.jpg"      ],      "estimatedCompletionDate": "2018-10-15T17:09:52+0900",      "isSuccess": true,      "requestDate": "2018-10-15T17:09:47+0900"    },    {      "cdnInstanceNo": "931582",      "purgeId": "915d238c-d051-11e8-a06c-995c49e861cf",      "isWholePurge": false,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [        "/sample_img.jpg"      ],      "estimatedCompletionDate": "2018-10-15T17:09:02+0900",      "isSuccess": true,      "requestDate": "2018-10-15T17:08:57+0900"    },    {      "cdnInstanceNo": "931582",      "purgeId": "4f483694-d051-11e8-bc6e-3fa9bcb46bdc",      "isWholePurge": false,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [        "/sample_img.jpg"      ],      "estimatedCompletionDate": "2018-10-15T17:07:11+0900",      "isSuccess": true,      "requestDate": "2018-10-15T17:07:06+0900"    },    {      "cdnInstanceNo": "931582",      "purgeId": "f3c40168-c867-11e8-9f09-970b5c3327c0",      "isWholePurge": true,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [],      "estimatedCompletionDate": "2018-10-05T15:33:02+0900",      "isSuccess": true,      "requestDate": "2018-10-05T15:29:02+0900"    },    {      "cdnInstanceNo": "931582",      "purgeId": "ab6580ae-c867-11e8-8087-a74bca8a3bb7",      "isWholePurge": true,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [],      "estimatedCompletionDate": "2018-10-05T15:31:01+0900",      "isSuccess": true,      "requestDate": "2018-10-05T15:27:01+0900"    }  ]}}

Java(v2)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class MainClass {
    private final static String API_END_POINT = "https://ncloud.apigw.ntruss.com";
    private final static String getCdnPlusPurgeHistoryList = "/cdn/v1/getGlobalCdnPurgeHistoryList";
    private final static String METHODS = "GET";

    private final static String API_GW_KEY = $ConsumerAPIGateWayKey$;
    private final static String ACCESS_KEY = $ConsumerAccessKey$;
    private final static String SECRET_KEY = $ConsumerSecretKey$;
    private final static String TIMESTAMP = Long.toString(Instant.now().toEpochMilli());


    public static void main(String args[]) throws Exception{

        Map<String, Object> requestParameters = new HashMap<String, Object>();
        requestParameters.put("cdnInstanceNo", $InstanceNo$);
        requestParameters.put("responseFormatType", "json");

        StringBuffer queryString = new StringBuffer();
        int index= 1;
        int requestParamSize = requestParameters.size();
        for (String key : requestParameters.keySet()) {
            if (index == 1) {
                queryString.append("?");
            }
            queryString.append(key);

            if (requestParameters.get(key) != null) {
                queryString.append('=');
                queryString.append(URLEncoder.encode(requestParameters.get(key).toString(), "UTF-8"));
            }
            if (index!= requestParamSize) {
                queryString.append("&");
                index ++;
            }
        }

        String openURL = API_END_POINT + getCdnPlusPurgeHistoryList + queryString;
        URL obj = new URL(openURL);

        HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("x-ncp-apigw-timestamp", TIMESTAMP);
        conn.setRequestProperty("x-ncp-apigw-api-key", API_GW_KEY);
        conn.setRequestProperty("x-ncp-iam-access-key", ACCESS_KEY);
        conn.setRequestProperty("x-ncp-apigw-signature-v2", makeSignatureV2(getCdnPlusPurgeHistoryList + queryString, ACCESS_KEY, SECRET_KEY, TIMESTAMP, METHODS));
        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);

        int responseCode = conn.getResponseCode();
        System.out.println("ResponseCode : " + responseCode);

        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader resultReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            StringBuffer requestResult = new StringBuffer();
            String readString = null;
            while ((readString = resultReader.readLine()) != null) {
                requestResult.append(readString);
            }
            System.out.println(requestResult.toString());
        }

    }
}

メインメソッドを実行すると、その結果が得られます。

{"getGlobalCdnPurgeHistoryListResponse": {  "requestId": "2414b1a9-b63c-414f-8007-49d7635465e7",  "returnCode": "0",  "returnMessage": "success",  "totalRows": 5,  "globalCdnPurgeHistoryList": [    {      "cdnInstanceNo": "931582",      "purgeId": "ae866ef0-d051-11e8-b3c7-57140a645b34",      "isWholePurge": false,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [        "/sample_img.jpg"      ],      "estimatedCompletionDate": "2018-10-15T17:09:52+0900",      "isSuccess": true,      "requestDate": "2018-10-15T17:09:47+0900"    },    {      "cdnInstanceNo": "931582",      "purgeId": "915d238c-d051-11e8-a06c-995c49e861cf",      "isWholePurge": false,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [        "/sample_img.jpg"      ],      "estimatedCompletionDate": "2018-10-15T17:09:02+0900",      "isSuccess": true,      "requestDate": "2018-10-15T17:08:57+0900"    },    {      "cdnInstanceNo": "931582",      "purgeId": "4f483694-d051-11e8-bc6e-3fa9bcb46bdc",      "isWholePurge": false,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [        "/sample_img.jpg"      ],      "estimatedCompletionDate": "2018-10-15T17:07:11+0900",      "isSuccess": true,      "requestDate": "2018-10-15T17:07:06+0900"    },    {      "cdnInstanceNo": "931582",      "purgeId": "f3c40168-c867-11e8-9f09-970b5c3327c0",      "isWholePurge": true,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [],      "estimatedCompletionDate": "2018-10-05T15:33:02+0900",      "isSuccess": true,      "requestDate": "2018-10-05T15:29:02+0900"    },    {      "cdnInstanceNo": "931582",      "purgeId": "ab6580ae-c867-11e8-8087-a74bca8a3bb7",      "isWholePurge": true,      "isWholeDomain": true,      "globalCdnServiceDomainList": [        {          "serviceDomainTypeCode": "DEFAULT",          "protocolTypeCode": "HTTP",          "defaultDomainName": "xinzbnndstxn931582.gcdn.ntruss.com",          "userDomainName": ""        }      ],      "targetFileList": [],      "estimatedCompletionDate": "2018-10-05T15:31:01+0900",      "isSuccess": true,      "requestDate": "2018-10-05T15:27:01+0900"    }  ]}}

結果実行確認

以下の画像のようにPurgeを実行した結果を確認することができます。

CDNにURLをリクエストし、キャッシュの初期化とコンテンツの変更事項を確認します。

関連情報リンク

次のガイドから関連情報が確認できます。

に対する検索結果は~件です。 ""

    に対する検索結果がありません。 ""

    処理中...