Query endpoint information (Java SDK V2)
Query all supported regions and endpoints by calling the DescribeRegions operation with OSS Java SDK V2.
Prerequisites
-
OSS Java SDK V2 is installed. If you use Maven, add the
com.aliyun:alibabacloud-oss-v2dependency to pom.xml. -
Access credentials are configured by using environment variables. The sample code in this topic uses
EnvironmentVariableCredentialsProviderto read credentials from theOSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables.
Permissions
An Alibaba Cloud account has full permissions by default. RAM users and RAM roles under the Alibaba Cloud account have no permissions by default. An Alibaba Cloud account or an account administrator must grant the required permissions.
|
API |
Action |
Description |
|
DescribeRegions |
|
Queries the endpoint information of all supported regions. |
Sample code
import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;
public class DescribeRegionsSample {
public static void main(String[] args) throws Exception {
String region = "cn-hangzhou";
CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
OSSClientBuilder clientBuilder = OSSClient.newBuilder()
.credentialsProvider(provider)
.region(region);
try (OSSClient client = clientBuilder.build()) {
DescribeRegionsResult result = client.describeRegions(
DescribeRegionsRequest.newBuilder().build());
System.out.printf("status code:%d, request id:%s%n",
result.statusCode(), result.requestId());
RegionInfoList regionInfoList = result.regionInfoList();
if (regionInfoList != null && regionInfoList.regionInfos() != null) {
for (RegionInfo regionInfo : regionInfoList.regionInfos()) {
System.out.printf("Region: %s, Internet: %s, Internal: %s%n",
regionInfo.region(), regionInfo.internetEndpoint(),
regionInfo.internalEndpoint());
}
}
} catch (Exception e) {
System.out.printf("error:%n%s", e);
}
}
}
References
-
For the synchronous sample code, see DescribeRegions.java.
-
For the asynchronous sample code, see DescribeRegionsAsync.java.