1. 准备工作
在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问访问密钥(AppKey)。
<a name="
2. 将 SDK添加到项目中” class=”reference-link”>
2. 将 SDK添加到项目中
1)pom.xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.5.1</version>
</dependency>
2、CreateTask
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
public class Demo1 {
//DefaultProfile.getProfile的参数分别是地域,access_key_id, access_key_secret
public static DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LT********", "CB********");
// public static DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "", "");
public static DefaultAcsClient client = new DefaultAcsClient(profile);
public static void main(String[] args) throws ClientException {
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
request.setDomain("retailir.cn-hangzhou.aliyuncs.com");
request.setVersion("2019-09-06");
request.setAction("CreateTask");
request.putBodyParameter("Appkey", "71********");
request.putBodyParameter("TaskName", "demotest1");
request.putBodyParameter("ImageLeftTop", "https://img2.ladyww.cn/cloudads/2023/01/31/3xu15cas1ce.png");
CommonResponse response = null;
try {
response = client.getCommonResponse(request);
} catch (ClientException e) {
e.printStackTrace();
}
System.out.println(response.getData());
}
}
3、GetTask
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
public class Demo2 {
//DefaultProfile.getProfile的参数分别是地域,access_key_id, access_key_secret
public static DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LT********", "CB********");
public static DefaultAcsClient client = new DefaultAcsClient(profile);
public static void main(String[] args) throws ClientException {
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
request.setDomain("retailir.cn-hangzhou.aliyuncs.com");
request.setVersion("2019-09-06");
request.setAction("GetTask");
request.putBodyParameter("Appkey", "71********");
request.putBodyParameter("JobId", "cb89d9cd4ae043bc835797708e4ce92a");
CommonResponse response = null;
try {
response = client.getCommonResponse(request);
} catch (ClientException e) {
e.printStackTrace();
}
System.out.println(response.getData());
}
}
原创文章,作者:网友投稿,如若转载,请注明出处:https://www.cloudads.cn/archives/33465.html