获取client可参考发送邮件示例。IAcsClientUtil类,示例如下。
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
public class IAcsClientUtil {
private static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
private static String secret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
private static String product = "Dm";
private static String regionId_hangzhou = "cn-hangzhou";//杭州
private static String accountName = "test***@example.net";
private static String regionId = "cn-hangzhou";//杭州
private static String endpointName = "";
private static String domain = "";
//新加坡
// private static String regionId = "ap-southeast-1";
// private static String endpointName = "dm.ap-southeast-1.aliyuncs.com";
// private static String domain = "dm.ap-southeast-1.aliyuncs.com";
protected static IAcsClient getiAcsClient() {
// 如果是除杭州region外的其它region(如新加坡等),需要将下面的"cn-hangzhou"替换为"ap-southeast-1"等,具体请参考API服务地址。
IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
if (!regionId_hangzhou.equals(regionId)) {
// 如果是除杭州region外的其它region(如新加坡region), 需要做如下处理
try {
DefaultProfile.addEndpoint(endpointName, regionId, product, domain);
} catch (ClientException e) {
e.printStackTrace();
}
}
return new DefaultAcsClient(profile);
}
}
说明
如何将AccessKeySecret和AccessKeyId配置到环境变量中,请参考:在环境变量中配置身份验证AccessKey
如何获取其他区域的RegionId,请参考:API服务地址
文档内容是否对您有帮助?