如何使用REST API Java使Google CDN缓存无效?

问题描述 投票:0回答:1

我一直在研究一个可行的例子,但是还没有找到。

我引用了以下链接Stackoverflow LinkGoogle Official Docs

从这些文档中,我确实了解到我需要实现这一点

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.compute.Compute;
import com.google.api.services.compute.model.CacheInvalidationRule;
import com.google.api.services.compute.model.Operation;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;

public class ComputeExample {
  public static void main(String args[]) throws IOException, GeneralSecurityException {
    // Project ID for this request.
    String project = "my-project"; // TODO: Update placeholder value.

    // Name of the UrlMap scoping this request.
    String urlMap = "my-url-map"; // TODO: Update placeholder value.

    // TODO: Assign values to desired fields of `requestBody`:
    CacheInvalidationRule requestBody = new CacheInvalidationRule();

    Compute computeService = createComputeService();
    Compute.UrlMaps.InvalidateCache request =
        computeService.urlMaps().invalidateCache(project, urlMap, requestBody);

    Operation response = request.execute();

    // TODO: Change code below to process the `response` object:
    System.out.println(response);
  }

  public static Compute createComputeService() throws IOException, GeneralSecurityException {
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

    GoogleCredential credential = GoogleCredential.getApplicationDefault();
    if (credential.createScopedRequired()) {
      credential =
          credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
    }

    return new Compute.Builder(httpTransport, jsonFactory, credential)
        .setApplicationName("Google-ComputeSample/0.1")
        .build();
  }
}

但是,如果您看到此示例,则它仅在其位置使用占位符值。

如果我要清空页面调用https://mywebsite.com/homepage.html我要在上述代码中输入什么信息?

我是否在这里添加了

 credential.createScoped(Arrays.asList("https://mywebsite.com/homepage.html"));

或者我应该在UrlMaps中添加它吗?这非常令人困惑。

java google-cloud-platform google-compute-api
1个回答
0
投票

它应该放在请求正文中。请求正文contains data with the following structure

JSON representation
{
  "path": string,
  "host": string
}

这些字段具有以下内容:

  • 作为字符串的路径
  • 主机作为字符串

如果设置,此无效规则将仅适用于与主机头匹配主机的请求。

您可能需要创建requestbody对象

CacheInvalidationRule requestBody = new CacheInvalidationRule();

这应该创建cacheinvalidationrule对象并分配给requestBody

此外,您可能还需要这样的东西

requestBody.setHostand requestBody.setPath = ""

这两个属性以字符串作为参数

requestBody.setHost=" mywebsite.com"

requestBody.setPath = "/homepage.html"

希望有帮助,祝你好运

© www.soinside.com 2019 - 2024. All rights reserved.