This topic describes how to install the RAM SDK for Java and provides an example about how to use the SDK.

Background information

Before using a RAM SDK, you must install a core Alibaba Cloud SDK and a RAM SDK.

  • The package name of the core Alibaba Cloud SDK for Java is aliyun-java-sdk-core. You can use OpenAPI Explorer to generate sample code and perform debug operations.
  • The package name of the RAM SDK for Java is aliyun-java-sdk-ram. For information about RAM API operations, see API Reference (RAM).

Install the SDKs for Java

Add dependencies on the SDK packages by using Maven, or download the SDK packages and then add them to your project.

  • Recommended. Add package dependencies by using Maven.
    1. Use Maven to create a project.
      mvn archetype:generate -DgroupId=com.aliyun.ram.sample \
      -DartifactId=ram-sdk-sample \
      -Dpackage=com.aliyun.ram.sample \
      -Dversion=1.0-SNAPSHOT
    2. Add the dependencies inside the dependencies element of the pom.xml file in the project.

      The following code is an example of the dependencies on the aliyun-java-sdk packages. In this example, the SDK version is 2.0.7.

      <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>aliyun-java-sdk-ram</artifactId>
          <version>2.0.7</version>
      </dependency>
      <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>aliyun-java-sdk-core</artifactId>
          <version>4.4.6</version>
      </dependency>
      Note
      Note the following information of the packages:
      • aliyun-java-sdk packages have been added to the Maven repository. You do not need to edit the settings.xml file.
      • You can visit the Maven repository to obtain the latest version of the aliyun-java-sdk-core package.
  • Download the JAR packages of the core Alibaba Cloud SDK and the RAM SDK, and then add the packages to your project.

    Download URLs:

Example of using the RAM SDK for Java

The following sample code uses the RAM SDK for Java to create a RAM user.

package com.aliyun.ram.sample;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

//The version of the RAM API was released on May 1, 2015.
import com.aliyuncs.ram.model.v20150501. *;

/**
 * Created by JasonGao on 15/11/4.
 */

public class RamServiceSample {

    public static void main(String[] args) {
        // Construct an Alibaba Cloud client. The client will be used to send a request.
        // When you construct the client, set the AccessKey ID and AccessKey secret.
        // RAM is a global service. Its API endpoint is located in the China (Hangzhou) region. Therefore, specify cn-hangzhou.
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                                                           "<AccessKeyId>",
                                                           "<AccessKeySecret>");
        DefaultAcsClient client = new DefaultAcsClient(profile);

        // Construct a CreateUser request.
        final CreateUserRequest request = new CreateUserRequest();

        // Specify the UserName parameter.
        request.setUserName("alice");

        // Issue the request and obtain a response.
        try {
            final CreateUserResponse response = client.getAcsResponse(request);

            System.out.println("UserName: " + response.getUser().getUserName());
            System.out.println("CreateTime: " + response.getUser().getCreateDate());
        } catch (ClientException e) {
            System.out.println("Failed.") ;
            System.out.println("Error code: " + e.getErrCode());
            System.out.println("Error message: " + e.getErrMsg());
        }

    }
}