mPaaS provides a Java SDK for offline packages. You can call OpenAPI to configure, create, publish, and manage these packages.
If you call OpenAPI using STS, see Call OpenAPI using STS.
Preparations
Before you use OpenAPI, you must obtain an AccessKey, App ID, Workspace ID, and Tenant ID. You also need to configure Maven dependencies and file uploads.
Get an AccessKey
An AccessKey consists of an AccessKey ID and an AccessKey secret. To learn how to obtain them, click here.
AccessKey ID: Identifies the user.
AccessKey secret: Authenticates the user's identity. You must keep it confidential.
Get the App ID, Workspace ID, and Tenant ID
Log on to the mPaaS console and select your application.
On the Overview page, click Code configuration. Select Android or iOS as needed, and then click Download configuration file > Download Now. The App ID, Workspace ID, and Tenant ID are displayed in the Code configuration window that appears.
Configure Maven dependencies
Before you use OpenAPI, you must add the following Maven dependency.
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>mpaas20201028</artifactId>
<version>5.0.0</version>
</dependency>Environment variable configuration
Configure the MPAAS_AK_ENV and MPAAS_SK_ENV environment variables.
For Linux and macOS, run the following commands:
export MPAAS_AK_ENV=<ACCESS_KEY_ID> export MPAAS_SK_ENV=<ACCESS_KEY_SECRET>NoteReplace
<ACCESS_KEY_ID>with your AccessKey ID and<ACCESS_KEY_SECRET>with your AccessKey secret.Windows system configuration
Create the MPAAS_AK_ENV and MPAAS_SK_ENV environment variables, and set their values to your AccessKey ID and AccessKey secret.
Restart Windows.
Usage example
import com.aliyun.mpaas20201028.Client;
import com.aliyun.mpaas20201028.models.QueryMcubeVhostRequest;
import com.aliyun.mpaas20201028.models.QueryMcubeVhostResponse;
public class MpaasApiDemo {
/**
* The App ID in the mPaaS console.
*/
private static final String APP_ID = "ALIPUB40DXXXXXXX";
/**
* The workspace ID in the mPaaS console.
*/
private static final String WORKSPACE_ID = "default";
/**
* The tenant ID in the mPaaS console.
*/
private static final String TENANT_ID = "XVXXXXXF";
/**
* The Endpoint to call.
*/
private static final String END_POINT = "mpaas.cn-hangzhou.aliyuncs.com";
public static void main(String[] args) throws Exception {
// An Alibaba Cloud account AccessKey has full access permissions to all APIs. Use a Resource Access Management (RAM) user for API access and daily O&M.
// Do not hard-code your AccessKey ID and AccessKey secret into your project code. This can lead to an AccessKey leak and compromise the security of all resources in your account.
// This example shows how to store the AccessKey ID and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
String accessKeyId = System.getenv("MPAAS_AK_ENV");
String accessKeySecret = System.getenv("MPAAS_SK_ENV");
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret)
.setEndpoint(END_POINT);
Client client = new Client(config);
QueryMcubeVhostRequest queryMcubeVhostRequest = new QueryMcubeVhostRequest();
queryMcubeVhostRequest.setAppId(APP_ID);
queryMcubeVhostRequest.setWorkspaceId(WORKSPACE_ID);
queryMcubeVhostRequest.setTenantId(TENANT_ID);
QueryMcubeVhostResponse acsResponse = null;
try {
QueryMcubeVhostResponse queryMcubeVhostResponse = client.queryMcubeVhost(queryMcubeVhostRequest);
System.out.println(queryMcubeVhostResponse.getBody().getResultCode());
System.out.println(queryMcubeVhostResponse.getBody().getQueryVhostResult());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Configuration file upload
API calls do not support file streams. You must first upload the file to Object Storage Service (OSS) using the upload utility class. Then, pass the returned OSS address as a parameter to the API.
Download the file upload utility class: OssPostObject.java.zip.
Usage example
The following example shows how to upload a file:
GetMcubeFileTokenRequest getMcubeFileTokenRequest = new GetMcubeFileTokenRequest();
getMcubeFileTokenRequest.setAppId(APP_ID);
getMcubeFileTokenRequest.setOnexFlag(true);
getMcubeFileTokenRequest.setTenantId(TENANT_ID);
getMcubeFileTokenRequest.setWorkspaceId(WORKSPACE_ID);
GetMcubeFileTokenResponse acsResponse = client.getMcubeFileToken(getMcubeFileTokenRequest);
System.out.println(JSON.toJSONString(acsResponse));
GetMcubeFileTokenResponseBody.GetMcubeFileTokenResponseBodyGetFileTokenResultFileToken fileToken = acsResponse.getBody().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();For more information about GetMcubeFileTokenRequest, see Get an upload file token.