一、问题描述
最近使用aliyun提供的java的CDN接口API来刷新缓存,导入其aliyun-java-sdk-2.2.3.jar和aliyun-java-sdk-core-3.0.6.jar后(更多aliyun依赖包和源码下载),进行测试报“com.aliyuncs.exceptions.ClientException: SDK.InvalidRegionId : Can not find endpoint to access.”异常问题,具体内容如下
com.aliyuncs.exceptions.ClientException: SDK.InvalidRegionId : Can not find endpoint to access.@b@ at com.aliyuncs.DefaultAcsClient.doAction(DefaultAcsClient.java:180)@b@ at com.aliyuncs.DefaultAcsClient.doAction(DefaultAcsClient.java:143)@b@ at com.aliyuncs.DefaultAcsClient.doAction(DefaultAcsClient.java:59)@b@ at com.xwood.cdn.AliyunCDNUTest.describeRefreshQuota(AliyunCDNUTest.java:133)@b@ at com.xwood.cdn.AliyunCDNUTest.main(AliyunCDNUTest.java:148)
测试类AliyunCDNUTest
import com.aliyuncs.DefaultAcsClient;@b@import com.aliyuncs.IAcsClient;@b@import com.aliyuncs.cdn.model.v20141111.DescribeRefreshQuotaRequest;@b@import com.aliyuncs.cdn.model.v20141111.DescribeRefreshTasksRequest;@b@import com.aliyuncs.cdn.model.v20141111.PurgeObjectCachesRequest;@b@import com.aliyuncs.cdn.model.v20141111.RefreshObjectCachesRequest;@b@import com.aliyuncs.exceptions.ClientException;@b@import com.aliyuncs.exceptions.ServerException;@b@import com.aliyuncs.http.FormatType;@b@import com.aliyuncs.http.HttpResponse;@b@import com.aliyuncs.profile.DefaultProfile;@b@import com.aliyuncs.profile.IClientProfile;@b@ @b@public class AliyunCDNUTest{@b@ @b@ private static IAcsClient client=new DefaultAcsClient(); @b@ @b@ private static String accessKeyID="xxxxxx111";@b@ private static String accessKeySecret="xxxxxxxxxxxx2222222222222";@b@ private static String res_url="http://www.xwood.net/images/xwood_logo_well.gif";@b@ @b@ private static String domain="www.xwood.net";@b@ private static String domain_path="/images/xwood_logo_well.gif";@b@ @b@ static{@b@ init();@b@ }@b@ @b@ public static void init(){@b@ try {@b@ IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",accessKeyID,accessKeySecret);@b@ client= new DefaultAcsClient(profile);@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ }@b@ }@b@ @b@ /**@b@ * 刷新资源方式-域名和资源路径分开@b@ */@b@ public static void purgeObjectCaches() {@b@ PurgeObjectCachesRequest request = new PurgeObjectCachesRequest();@b@ //要刷新的域名@b@ request.setDomainName(domain);@b@ //要刷新的文件路径@b@ request.setObjectPath(domain_path);@b@ //刷新类型,默认是File,刷新目录为Directory@b@ request.setObjectType("File");@b@ //设置返回格式为JSON@b@ request.setAcceptFormat(FormatType.JSON);@b@ try {@b@ HttpResponse httpResponse = client.doAction(request);@b@ System.out.println(httpResponse.getUrl());@b@ System.out.println(new String(httpResponse.getContent()));@b@ System.out.println(httpResponse.getStatus());@b@ } catch (ServerException e) {@b@ e.printStackTrace();@b@ } catch (ClientException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@@b@ /**@b@ * 刷新资源方式-域名和资源路径不分开@b@ */@b@ public static void refreshObjectCaches() {@b@ RefreshObjectCachesRequest request = new RefreshObjectCachesRequest();@b@ //要刷新的URI@b@ request.setObjectPath(res_url);@b@ //刷新类型,默认是File,刷新目录为Directory@b@ request.setObjectType("File");@b@ //设置返回格式为JSON@b@ request.setAcceptFormat(FormatType.JSON);@b@@b@ try {@b@ HttpResponse httpResponse = client.doAction(request);@b@ System.out.println(httpResponse.getUrl());@b@ System.out.println(new String(httpResponse.getContent()));@b@ System.out.println(httpResponse.getStatus());@b@ } catch (ServerException e) {@b@ e.printStackTrace();@b@ } catch (ClientException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@@b@ /**@b@ * 查询资源刷新纪录@b@ */@b@ public static void describeRefreshTasks() {@b@ DescribeRefreshTasksRequest request = new DescribeRefreshTasksRequest();@b@ request.setObjectPath(res_url);@b@ request.setPageSize(10);@b@ request.setPageNumber(1);@b@ //设置返回格式为JSON@b@ request.setAcceptFormat(FormatType.JSON);@b@@b@ try {@b@ HttpResponse httpResponse = client.doAction(request);@b@ System.out.println(httpResponse.getUrl());@b@ System.out.println(new String(httpResponse.getContent()));@b@ System.out.println(httpResponse.getStatus());@b@ } catch (ServerException e) {@b@ // TODO Auto-generated catch block@b@ e.printStackTrace();@b@ } catch (ClientException e) {@b@ // TODO Auto-generated catch block@b@ e.printStackTrace();@b@ }@b@@b@ }@b@@b@ /**@b@ * 查询CDN刷新剩余量@b@ */@b@ public static void describeRefreshQuota() {@b@ DescribeRefreshQuotaRequest request = new DescribeRefreshQuotaRequest();@b@ //设置返回格式为JSON@b@ request.setAcceptFormat(FormatType.JSON);@b@ try {@b@ HttpResponse httpResponse = client.doAction(request);@b@ System.out.println(httpResponse.getUrl());@b@ System.out.println(new String(httpResponse.getContent()));@b@ System.out.println(httpResponse.getStatus());@b@ } catch (ServerException e) {@b@ // TODO Auto-generated catch block@b@ e.printStackTrace();@b@ } catch (ClientException e) {@b@ // TODO Auto-generated catch block@b@ e.printStackTrace();@b@ }@b@@b@ }@b@@b@ public static void main(String[] args) {@b@ AliyunCDNUTest.refreshObjectCaches();@b@ }@b@@b@}
二、解决方法
之前在初始化方法init()在初始化客户端Profile没有配置正确的区域参数,如上面的接入点Endpoint配置的为“cn-hangzhou”,如下图所示,还有“c-shenzhen”..."cn-shanghai"