|
| 1 | +English | [中文](README.md) |
| 2 | + |
| 3 | +# Volcengine SDK for Java |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +* Requirements |
| 8 | +* Usage |
| 9 | +* API Docs |
| 10 | +* Notes |
| 11 | + |
| 12 | +### Requirements |
| 13 | + |
| 14 | +The SDK requires **Java 1.8.0_131+**. You can download the latest version from: http://developers.sun.com/downloads/. |
| 15 | + |
| 16 | +If your Java version is **Java 9 or later**, add `javax.annotation-api` because it was removed from JDK 9+. |
| 17 | + |
| 18 | +```xml |
| 19 | +<dependency> |
| 20 | + <groupId>javax.annotation</groupId> |
| 21 | + <artifactId>javax.annotation-api</artifactId> |
| 22 | + <version>1.3.2</version> |
| 23 | +</dependency> |
| 24 | +``` |
| 25 | + |
| 26 | +### Usage |
| 27 | + |
| 28 | +* Getting Started |
| 29 | +* Example |
| 30 | + |
| 31 | +#### Getting Started |
| 32 | + |
| 33 | +##### Installation |
| 34 | + |
| 35 | +It is recommended to use Maven. Add dependencies for the modules you need. |
| 36 | + |
| 37 | +##### Init Maven `settings.xml` |
| 38 | + |
| 39 | +If your version is greater than **0.1.27**, you can directly use Maven Central. |
| 40 | + |
| 41 | +If your version is **0.1.27 or earlier**, you need ByteDance Maven repository. In `conf/settings.xml`, under `<mirrors/>`, add: |
| 42 | + |
| 43 | +```xml |
| 44 | +<mirror> |
| 45 | + <id>bytedanceMaven</id> |
| 46 | + <mirrorOf>my-repo-id</mirrorOf> |
| 47 | + <name>ByteDance Maven Repository</name> |
| 48 | + <url>https://artifact.bytedance.com/repository/releases/</url> |
| 49 | +</mirror> |
| 50 | +``` |
| 51 | + |
| 52 | +##### Importing the BOM |
| 53 | + |
| 54 | +```xml |
| 55 | +<dependencyManagement> |
| 56 | + <dependencies> |
| 57 | + <dependency> |
| 58 | + <groupId>com.volcengine</groupId> |
| 59 | + <artifactId>volcengine-java-sdk-bom</artifactId> |
| 60 | + <version>1.0.3</version> |
| 61 | + <type>pom</type> |
| 62 | + <scope>import</scope> |
| 63 | + </dependency> |
| 64 | + </dependencies> |
| 65 | +</dependencyManagement> |
| 66 | +``` |
| 67 | + |
| 68 | +##### Using SDK Maven modules |
| 69 | + |
| 70 | +```xml |
| 71 | +<dependencies> |
| 72 | + <dependency> |
| 73 | + <groupId>com.volcengine</groupId> |
| 74 | + <artifactId>volcengine-java-sdk-vpc</artifactId> |
| 75 | + <version>1.0.3</version> |
| 76 | + </dependency> |
| 77 | + <dependency> |
| 78 | + <groupId>com.volcengine</groupId> |
| 79 | + <artifactId>volcengine-java-sdk-ecs</artifactId> |
| 80 | + <version>1.0.3</version> |
| 81 | + </dependency> |
| 82 | +</dependencies> |
| 83 | +``` |
| 84 | + |
| 85 | +##### Credentials |
| 86 | + |
| 87 | +**Import via environment variables**: |
| 88 | + |
| 89 | +``` |
| 90 | +export VOLCENGINE_ACCESS_KEY=your ak |
| 91 | +export VOLCENGINE_SECRET_KEY=your sk |
| 92 | +# If using token |
| 93 | +export VOLCENGINE_SESSION_TOKEN=token |
| 94 | +``` |
| 95 | + |
| 96 | +**Import in code**: |
| 97 | + |
| 98 | +```java |
| 99 | +Credentials credentials = Credentials.getCredentials(ak, sk); |
| 100 | +// If using token |
| 101 | +Credentials credentials = Credentials.getCredentials(ak, sk, token); |
| 102 | +``` |
| 103 | + |
| 104 | +##### Endpoint |
| 105 | + |
| 106 | +To customize the SDK endpoint: |
| 107 | + |
| 108 | +```java |
| 109 | +ApiClient apiClient = new ApiClient() |
| 110 | + .setCredentials(Credentials.getCredentials(ak, sk)) |
| 111 | + .setRegion(region).setEndpoint("ecs.cn-beijing-autodriving.volcengineapi.com"); |
| 112 | +``` |
| 113 | + |
| 114 | +Standard endpoint rules: |
| 115 | + |
| 116 | +| Regional Service | Global Service | |
| 117 | +|---|---| |
| 118 | +| `{service}.{region}.volcengineapi.com` <br> e.g. `ecs.cn-beijing-autodriving.volcengineapi.com` | `{service}.volcengineapi.com` <br> e.g. `iam.volcengineapi.com` | |
| 119 | + |
| 120 | +Note: |
| 121 | + |
| 122 | +- If the service name contains `_`, it should be converted to `-` in the endpoint. Use lowercase for all characters. |
| 123 | + |
| 124 | +#### SDK Example |
| 125 | + |
| 126 | +```java |
| 127 | +import com.volcengine.ApiClient; |
| 128 | +import com.volcengine.ApiException; |
| 129 | +import com.volcengine.sign.Credentials; |
| 130 | +import com.volcengine.vpc.VpcApi; |
| 131 | +import com.volcengine.vpc.model.DescribeVpcsRequest; |
| 132 | +import com.volcengine.vpc.model.DescribeVpcsResponse; |
| 133 | + |
| 134 | +import java.util.ArrayList; |
| 135 | +import java.util.List; |
| 136 | + |
| 137 | +public class TestVpc { |
| 138 | + public static void main(String[] args) throws Exception { |
| 139 | + String ak = "your ak"; |
| 140 | + String sk = "your sk"; |
| 141 | + String region = "cn-beijing"; |
| 142 | + |
| 143 | + ApiClient apiClient = new ApiClient() |
| 144 | + .setCredentials(Credentials.getCredentials(ak, sk)) |
| 145 | + .setRegion(region); |
| 146 | + VpcApi vpcApi = new VpcApi(apiClient); |
| 147 | + DescribeVpcsRequest request = new DescribeVpcsRequest(); |
| 148 | + List<String> list = new ArrayList<>(); |
| 149 | + list.add("vpc-13fpdgwk7rxfk3n6nu44wisg7"); |
| 150 | + request.setVpcIds(list); |
| 151 | + try { |
| 152 | + DescribeVpcsResponse response = vpcApi.describeVpcs(request); |
| 153 | + System.out.println(response); |
| 154 | + } catch (ApiException e) { |
| 155 | + System.out.println(e.getResponseBody()); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +For more examples, see: [SDK Integration Guide](./SDK_Integration.md) |
0 commit comments