All Products
Search
Document Center

Mobile Platform as a Service:Overview and preparation

Last Updated:Sep 07, 2023

MDS provides Java SDK for offline package release, enabling developers to configure, create, release, and manage offline packages by calling the API.

Preparation

Before using the open API, you need to obtain AccessKey, App ID, Workspace ID and Tenant ID, configure Maven dependencies and configure file uploading.

Obtain AccessKey

AccessKey includes AccessKey ID and AccessKey Secret. Click here for obtaining method.

  • AccessKey ID: used to identify users.

  • AccessKey Secret: used for user authentication. MUST be kept safe.

Obtain App ID, Workspace ID and Tenant ID

  1. Log in to mPaaS console, and enter the App.

  2. In Overview page, click Code configurations (choose Android or iOS based on your needs)> Download configuration file > Download now. You can view App ID and Workspace ID in the Code configurations panel.

Configure Maven dependencies

Before using the API, you need to complete the following Maven dependency configurations.

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-mpaas</artifactId>
  <version>3.0.5</version>
</dependency>

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <optional>true</optional>
  <version>[4.3.2,5.0.0)</version>
</dependency>

Environment Variable Configuration

Configure environment variable MPAAS_AK_ENV and MPAAS_SK_ENV

  • Linux and macOS system configuration methods execute the following commands:

    export MPAAS_AK_ENV=<access_key_id>
    export MPAAS_SK_ENV=<access_key_secret>
    Note

    access_key_id is replaced with the prepared AccessKey ID, and access_key_secret is replaced with the AccessKey Secret.

  • Windows system configuration method

    1. Create a new environment variable, add environment variables MPAAS_AK_ENV and MPAAS_SK_ENV, and write the prepared AccessKey ID and AccessKey Secret.

    2. Restart Windows system.

Code sample

The following shows the code sample of using Client in Maven:

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.mpaas.model.v20201028.QueryMcubeVhostRequest;
import com.aliyuncs.mpaas.model.v20201028.QueryMcubeVhostResponse;
import com.aliyuncs.profile.DefaultProfile;

public class MpaasApiDemo {

    /**
     * The corresponding APP ID on the mPaaS console
     */
    private static final String APP_ID = "ALIPUB40DXXXXXXX";

    /**
     * The corresponding workspace id on the mPaaS console
     */
    private static final String WORKSPACE_ID = "default";

    /**
     * The corresponding tenant id on the mPaaS console
     */
    private static final String TENANT_ID = "XVXXXXXF";

    /**
     * Region ID, the default is cn-hangzhou
     */
    private static final String REGION_ID = "cn-hangzhou";

    /**
     * Product name
     */
    private static final String PRODUCT = "mpaas";

    /**
     * The endpoint to call
     */
    private static final String END_POINT = "mpaas.cn-hangzhou.aliyuncs.com";

    public static void main(String[] args) {
        // Alibaba Cloud account AccessKey has access rights to all APIs. It is recommended that you use RAM users for API access or daily operation and maintenance.
        // It is strongly recommended not to save the AccessKey ID and AccessKey Secret in the project code, otherwise the AccessKey may be leaked, threatening the security of all resources under your account.
        // This example uses saving the AccessKey ID and AccessKey Secret in environment variables as an example. You can also save it to the configuration file according to business needs.
        String accessKeyId = System.getenv("MPAAS_AK_ENV");
        String accessKeySecret = System.getenv("MPAAS_SK_ENV");

        DefaultProfile.addEndpoint(REGION_ID, PRODUCT, END_POINT);
        DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, accessKeyId, accessKeySecret);
        IAcsClient iAcsClient = new DefaultAcsClient(profile);
        QueryMcubeVhostRequest queryMcubeVhostRequest = new QueryMcubeVhostRequest();
        queryMcubeVhostRequest.setAppId(APP_ID);
        queryMcubeVhostRequest.setWorkspaceId(WORKSPACE_ID);
        queryMcubeVhostRequest.setTenantId(TENANT_ID);
        QueryMcubeVhostResponse acsResponse = null;
        try {
            acsResponse = iAcsClient.getAcsResponse(queryMcubeVhostRequest);
            System.out.println(acsResponse.getResultCode());
            System.out.println(acsResponse.getQueryVhostResult());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

Configure file uploading

Since file streaming is not allowed in all APIs, to upload a file, you need to upload it to OSS first by calling the upload tool class, and then send the returned OSS address as a parameter to the specified API.

You can download the file upload tool class OssPostObject.java.zip.

Code sample

The following shows the code sample of file uploading:

        GetMcubeFileTokenRequest getMcubeFileTokenRequest = new GetMcubeFileTokenRequest();
        getMcubeFileTokenRequest.setAppId(APP_ID);
        getMcubeFileTokenRequest.setOnexFlag(true);
        getMcubeFileTokenRequest.setTenantId(TENANT_ID);
        getMcubeFileTokenRequest.setWorkspaceId(WORKSPACE_ID);
        GetMcubeFileTokenResponse acsResponse = iAcsClient.getAcsResponse(getMcubeFileTokenRequest);
        System.out.println(JSON.toJSONString(acsResponse));

        GetMcubeFileTokenResponse.GetFileTokenResult.FileToken fileToken = acsResponse.getGetFileTokenResult().getFileToken();
        OssPostObject ossPostObject = new OssPostObject();
        ossPostObject.setKey(fileToken.getDir());
        ossPostObject.setHost(fileToken.getHost());
        ossPostObject.setOssAccessId(fileToken.getAccessid());
        ossPostObject.setPolicy(fileToken.getPolicy());
        ossPostObject.setSignature(fileToken.getSignature());
        ossPostObject.setFilePath("your/local/file/path");
        String s = ossPostObject.postObject();

Refer to Obtain upload file token for descriptions about GetMcubeFileTokenRequest.