构建 ODPS 客户端
本文档展示了如何使用多种认证方式构建阿里云MaxCompute(原ODPS)客户端实例。MaxCompute是一种大规模数据处理和分析服务,本SDK帮助用户快速集成和使用该服务。
目录
使用AccessKey构建客户端
此方法需要用户提供AccessId和AccessKey。
示例代码
public static Odps buildWithAccessKey(String accessId, String accessKey) {
Account account = new AliyunAccount(accessId, accessKey);
Odps odps = new Odps(account);
odps.setEndpoint(SAMPLE_ENDPOINT);
return odps;
}
参考链接
使用STS Token构建客户端
此 方法适用于临时授权场景。
示例代码
public static Odps buildWithStsToken(String accessId, String accessKey, String stsToken) {
Account account = new StsAccount(accessId, accessKey, stsToken);
Odps odps = new Odps(account);
odps.setEndpoint(SAMPLE_ENDPOINT);
return odps;
}
使用阿里云CredentialProvider构建客户端
此方法适用于如使用默认凭据链,或ECS实例上的RAM角色授权等场景。
示例代码
使用 credential-java 包
public static Odps buildWithCredentialProvider(AlibabaCloudCredentialsProvider credentialProvider) {
Account account = new AklessAccount(credentialProvider);
Odps odps = new Odps(account);
odps.setEndpoint(SAMPLE_ENDPOINT);
return odps;
}
使用 aliyun-java-auth 包
public static Odps buildWithCredentialProvider(ICredentialProvider credentialProvider) {
Account account = new AklessAccount(credentialProvider);
Odps odps = new Odps(account);
odps.setEndpoint(SAMPLE_ENDPOINT);
return odps;
}
说明
CredentialProvider 是阿里云提供的一种无AK认证方式,它实际上提供了一种基于STS Token的生成和自动轮换机制。
阿里云的credential-java
包提供了AlibabaCloudCredentialsProvider
接口和多种实现,
阿里云的aliyun-java-auth
包提供了ICredentialProvider
接口和多种实现,
用户可以根据需要选择不同的实现 。
使用双重签名认证构建客户端
某些应用需要双重签名认证(本质上是使用一组AK进行应用标识,另一组AK进行用户标识)。
示例代码
public static Odps buildWithDualSignature(String accessId, String accessKey, String appAccessId, String appAccessKey) {
AppAccount appAccount = new AppAccount(new AliyunAccount(appAccessId, appAccessKey));
Account account = new AliyunAccount(accessId, accessKey);
Odps odps = new Odps(account, appAccount);
odps.setEndpoint(SAMPLE_ENDPOINT);
return odps;
}
使用Bearer Token构建客户端
Bearer Tokens通常用于短期访问授权。
示例代码
public static Odps buildWithBearerToken(String bearerToken) {
Account account = new BearerTokenAccount(bearerToken);
Odps odps = new Odps(account);
odps.setEndpoint(SAMPLE_ENDPOINT);
return odps;
}
生成Bearer Token
Bearer Token的生成方法如下:
// 示例代码
SecurityManager sm = new SecurityManager();
String policy = "<your policy>";
String bearerToken = sm.generateAuthorizationToken(policy, "BEARER");