本文主要描述镜像仓库 Java 语言 SDK 的安装和配置,及使用 Java SDK 调用云端 API 的示例。
javax.annotation-api
。javax.annotation-api
被移除,因此需要在项目文件中加入以下依赖。<dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.2</version> </dependency>
推荐通过 Maven 管理和使用 JAVA SDK。
在本地使用 Maven 构建您的项目。
添加 Maven 项目依赖。
需要使用字节跳动的 Maven 仓库来完成依赖,请先编辑 Maven 安装目录的conf/setting.xml
文件,增加镜像配置。
<mirror> <id>bytedanceMaven</id> <mirrorOf>my-repo-id</mirrorOf> <name>字节跳动maven仓库</name> <url>https://artifact.bytedance.com/repository/releases/</url> </mirror>
导入并配置 pom.xml 文件。
<dependencyManagement> <dependencies> <dependency> <groupId>com.volcengine</groupId> <artifactId>volcengine-java-sdk-bom</artifactId> <version>0.1.6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
使用 SDK Maven 模块。
<dependency> <groupId>com.volcengine</groupId> <artifactId>volcengine-java-sdk-cr</artifactId> <version>1.0.75</version> </dependency>
镜像仓库支持如下两种证书配置方法:
export VOLCENGINE_ACCESS_KEY=your_ak // 您的 Access Key ID export VOLCENGINE_SECRET_KEY=your_sk // 您的 Secret Access Key
Credentials credentials = Credentials.getCredentials(ak,sk);
本文以查询实例接口 ListRegistries为例。
package com.volcengine.cr; import com.volcengine.ApiClient; import com.volcengine.ApiException; import com.volcengine.cr.model.FilterForListRegistriesInput; import com.volcengine.cr.model.ListRegistriesRequest; import com.volcengine.cr.model.ListRegistriesResponse; import com.volcengine.cr.model.StatusForListRegistriesInput; import com.volcengine.sign.Credentials; public class test { public static void main(String[] args) { String ak = "AKLT****"; // 您的 Access Key ID String sk = "WTJG****";// 您的 Secret Access Key String region = "cn-beijing";// 您的业务所在地域 ApiClient apiClient = new ApiClient() .setCredentials(Credentials.getCredentials(ak, sk)) .setRegion(region); CrApi crApi = new CrApi(apiClient); StatusForListRegistriesInput statusListRregistries = new StatusForListRegistriesInput(); statusListRregistries.setCondition("Ok"); statusListRregistries.setPhase("Running"); ListRegistriesRequest listRegistries = new ListRegistriesRequest(); listRegistries.setPageNumber((long) 1); listRegistries.setPageSize((long) 10); // 用例1 names_null_types_all_status_exist FilterForListRegistriesInput filterListRregistries = new FilterForListRegistriesInput(); filterListRregistries.addTypesItem("Basic"); filterListRregistries.addTypesItem("Enterprise"); filterListRregistries.addStatusesItem(statusListRregistries); listRegistries.setFilter(filterListRregistries); try { ListRegistriesResponse respListRegistries = crApi.listRegistries(listRegistries); System.out.println(listRegistries); System.out.println(respListRegistries); } catch (ApiException e) { System.out.println(e.getResponseBody()); } } }