【问题标题】:Purging cached urls in akamai through java code?通过 java 代码清除 akamai 中的缓存 URL?
【发布时间】:2019-02-11 00:55:51
【问题描述】:

在我的项目中,每当调用 API 时,它都会缓存在 Akamai 中。但是当客户端通过 UI 更改数据库中的某些内容时。我们需要在 AKAMAI 中使缓存的 API 响应无效,并用新的新鲜 json 数据填充它。我在互联网上找到了一些链接:akamai-purging 但我无法理解他们在谈论的这个链接中的 cp-code 是什么?

这是我给出的示例代码:405 Not Allowed

代码:

public static void main(String[] args) throws IOException, RequestSigningException {

    URL url = new URL("https://xxx-host-name-/scripts.4535eaf743502b25ba3a.js");

    HttpTransport HTTP_TRANSPORT = new ApacheHttpTransport();
    HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
    AkamaiPostData postData = new AkamaiPostData();
    postData.setHostname(AkamaiConstants.SITE_HOST_NAME);

    Gson gson = new Gson();
    String postDataJSON = gson.toJson(postData);
    byte[] contentBytes = postDataJSON.getBytes();
    HttpContent content = new ByteArrayContent("application/json", contentBytes);
    HttpRequest request = requestFactory.buildDeleteRequest(new GenericUrl(url));
    HttpHeaders headers = request.getHeaders();
    headers.set("Host", "xxx-host-name-");

    ClientCredential credential = new DefaultCredential(AkamaiConstants.CLIENT_TOKEN, AkamaiConstants.ACCESS_TOKEN, AkamaiConstants.CLIENT_SECRET);
    RequestSigner signer = new EdgeGridV1Signer(Collections.EMPTY_LIST, 1024 * 2);
    HttpRequest signedRequest = signer.sign(request, credential);
    HttpResponse response = signedRequest.execute();
    String result = response.parseAsString();
    System.out.println("result::" + result);
}

【问题讨论】:

    标签: java rest cdn akamai purge


    【解决方案1】:

    所以最后我能够做到这一点。我使用的这种方法是 Akamai 提供的快速清除方法。

    代码示例:

    public class AkamaiFastPurge {
    
        private static final String HTTPS = "https";
    
        public static void main(String... s) throws URISyntaxException, IOException, RequestSigningException {
    
    
            ClientCredential credential = ClientCredential.builder()
                    .accessToken("Your-access-token")
                    .clientToken("Your-client-token")
                    .clientSecret("Your-client-secret")
                    .host("Your-host")
                    .build();
    
    
            ArrayList arrayList = new ArrayList<String>();
    
            // You can add multiple urls.
            arrayList.add("*****-Actual-url-you-want-to-purge*****");
    
            HttpResponse response = invalidateUrls(credential, arrayList, "production");
    
            System.out.println(response.getStatusCode());
            System.out.println(response.getStatusMessage());
        }
    
        public static HttpResponse invalidateUrls(ClientCredential clientCredential, List<String> urls, String network) {
    
            HttpTransport httpTransport = new ApacheHttpTransport();
            HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
    
            HttpRequest request = null;
    
            try {
    
                // This is fast purge approach
                URI uri = new URI(HTTPS, "api.ccu.akamai.com", "/ccu/v3/invalidate/url/" + network, null, null);
    
                String requestBody = getStringRequestBody(urls);
    
                request = requestFactory.buildPostRequest(new GenericUrl(uri), ByteArrayContent.fromString("application/json", requestBody));
                GoogleHttpClientEdgeGridRequestSigner requestSigner = new GoogleHttpClientEdgeGridRequestSigner(clientCredential);
                requestSigner.sign(request);
    
                return request.execute();
    
            } catch (IOException e) {
    //            log.error("IOException in Akamai Utility", e);
            } catch (RequestSigningException e) {
    //            log.error("RequestSigningException in Akamai Utility", e);
            } catch (URISyntaxException e) {
    //            log.error("UriSyntaxException in Akamai Utility", e);
            }
    
            return null;
        }
    
        public static String getStringRequestBody(List<String> urls) {
    
            Map<String, List<String>> akamaiRequestMap = Maps.newHashMap();
            akamaiRequestMap.put("objects", urls);
    
            return new Gson().toJson(akamaiRequestMap);
        }
    }
    

    我使用的依赖项:

     <dependency>
            <groupId>com.akamai.edgegrid</groupId>
            <artifactId>edgegrid-signer-google-http-client</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
    </dependencies>
    

    【讨论】:

      【解决方案2】:

      在 Akamai 中清除内容需要 API 凭据。您可以在Akamai Developer 网站上了解有关Akamai Fast Purge API 的更多信息。您需要联系您的 Akamai 客户团队或在 Luna Control Center 中配置您需要的 API 访问权限。 API 文档将为您提供了解 API 以及如何配置适当凭据的链接。

      作为后续的一小部分,CP 代码是 Akamai 的唯一标识符,用于将特定域或服务与您的合同绑定以用于计费目的。除了访问 API 所需的凭据之外,您肯定需要知道 Akamai 支持的域正在使用的 CP 代码才能成功调用 Fast Purge API。

      【讨论】:

      • 感谢您的回复,我只是想知道它的步骤是什么。 1) 首先,我必须通过 Akamai 团队启用我的清除功能。 2) 基于令牌,我将清除我的 API。 3) 清除 API 意味着 Akamai 将自动使其无效并再次使用相同的 API 来获取新数据?如果我错了,请纠正我
      • 这基本上是正确的,除了您应该记住 Akamai 将根据最终用户请求访问 API。只有当请求到达 Akamai 时,才会将其转发到您的来源。
      猜你喜欢
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      • 1970-01-01
      • 2012-03-30
      相关资源
      最近更新 更多