|
| 1 | +/** |
| 2 | + * OdpsClientSetupSample.java |
| 3 | + * |
| 4 | + * This example class demonstrates how to construct Alibaba Cloud MaxCompute (formerly ODPS) client instances based on various authentication methods. |
| 5 | + * MaxCompute is a large-scale data processing and analytics service; this SDK facilitates rapid integration and usage of the service. |
| 6 | + */ |
| 7 | + |
| 8 | +import com.aliyun.auth.credentials.provider.ICredentialProvider; |
| 9 | +import com.aliyun.odps.Odps; |
| 10 | +import com.aliyun.odps.account.Account; |
| 11 | +import com.aliyun.odps.account.AklessAccount; |
| 12 | +import com.aliyun.odps.account.AliyunAccount; |
| 13 | +import com.aliyun.odps.account.AppAccount; |
| 14 | +import com.aliyun.odps.account.BearerTokenAccount; |
| 15 | +import com.aliyun.odps.account.StsAccount; |
| 16 | + |
| 17 | +/** |
| 18 | + * OdpsClientSetupSample provides static methods for constructing Odps clients, |
| 19 | + * supporting authentication via AccessKey, STS Token, Aliyun Credential Provider, Dual-Signature Authentication, and Bearer Token. |
| 20 | + */ |
| 21 | +public class OdpsClientSetupSample { |
| 22 | + // Replace this Endpoint with your actual MaxCompute service address |
| 23 | + private static final String SAMPLE_ENDPOINT = "<your odps endpoint>"; |
| 24 | + |
| 25 | + /** |
| 26 | + * Constructs an Odps client using AccessKey. |
| 27 | + * Requires users to provide an AccessId and AccessKey. |
| 28 | + * <a href="https://help.aliyun.com/zh/ram/user-guide/create-an-accesskey-pair">How to create and obtain AccessKey</a> |
| 29 | + * |
| 30 | + * @param accessId User's AccessId. |
| 31 | + * @param accessKey User's AccessKey. |
| 32 | + * @return An initialized Odps instance. |
| 33 | + */ |
| 34 | + public static Odps buildWithAccessKey(String accessId, String accessKey) { |
| 35 | + Account account = new AliyunAccount(accessId, accessKey); |
| 36 | + Odps odps = new Odps(account); |
| 37 | + odps.setEndpoint(SAMPLE_ENDPOINT); |
| 38 | + return odps; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Constructs an Odps client using STS Token. |
| 43 | + * Suitable for temporary authorization scenarios. |
| 44 | + * |
| 45 | + * @param accessId User's AccessId. |
| 46 | + * @param accessKey User's AccessKey. |
| 47 | + * @param stsToken The STS Token. |
| 48 | + * @return An initialized Odps instance. |
| 49 | + */ |
| 50 | + public static Odps buildWithStsToken(String accessId, String accessKey, String stsToken) { |
| 51 | + Account account = new StsAccount(accessId, accessKey, stsToken); |
| 52 | + Odps odps = new Odps(account); |
| 53 | + odps.setEndpoint(SAMPLE_ENDPOINT); |
| 54 | + return odps; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Constructs an Odps client using an Aliyun Credential Provider. |
| 59 | + * Suitable for scenarios such as RAM role authorization on ECS instances. |
| 60 | + * ICredentialProvider is an AK-less authentication method provided by Alibaba Cloud (it actually provides a mechanism for generating and automatically rotating STS Tokens based on RamRole, and for ODPS, it still falls under STS Token-based authentication). |
| 61 | + * The aliyun-java-auth package provides many implementations of ICredentialProvider, such as DefaultCredentialProvider and RamRoleArnCredentialProvider; users can choose different implementations based on their needs. |
| 62 | + * |
| 63 | + * @param credentialProvider An instance of Aliyun Credential Provider. |
| 64 | + * @return An initialized Odps instance. |
| 65 | + */ |
| 66 | + public static Odps buildWithCredentialProvider(ICredentialProvider credentialProvider) { |
| 67 | + Account account = new AklessAccount(credentialProvider); |
| 68 | + Odps odps = new Odps(account); |
| 69 | + odps.setEndpoint(SAMPLE_ENDPOINT); |
| 70 | + return odps; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Constructs an Odps client using Dual-Signature Authentication. |
| 75 | + * Some applications require dual-signature authentication (which essentially uses one set of AKs for application identification and another for user identification). |
| 76 | + * |
| 77 | + * @param accessId User's AccessId. |
| 78 | + * @param accessKey User's AccessKey. |
| 79 | + * @param appAccessId Application's AccessId. |
| 80 | + * @param appAccessKey Application's AccessKey. |
| 81 | + * @return An initialized Odps instance. |
| 82 | + */ |
| 83 | + public static Odps buildWithDualSignature(String accessId, String accessKey, String appAccessId, String appAccessKey) { |
| 84 | + AppAccount appAccount = new AppAccount(new AliyunAccount(appAccessId, appAccessKey)); |
| 85 | + Account account = new AliyunAccount(accessId, accessKey); |
| 86 | + Odps odps = new Odps(account, appAccount); |
| 87 | + odps.setEndpoint(SAMPLE_ENDPOINT); |
| 88 | + return odps; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Constructs an Odps client using Bearer Token. |
| 93 | + * Bearer Tokens are typically used for short-term access authorization. |
| 94 | + * How to generate Bearer Token: {@link com.aliyun.odps.security.SecurityManager#generateAuthorizationToken(String, String)} |
| 95 | + * Example: sm.generateAuthorizationToken(policy, "BEARER"); |
| 96 | + * |
| 97 | + * @param bearerToken The generated Bearer Token string. |
| 98 | + * @return An initialized Odps instance. |
| 99 | + */ |
| 100 | + public static Odps buildWithBearerToken(String bearerToken) { |
| 101 | + Account account = new BearerTokenAccount(bearerToken); |
| 102 | + Odps odps = new Odps(account); |
| 103 | + odps.setEndpoint(SAMPLE_ENDPOINT); |
| 104 | + return odps; |
| 105 | + } |
| 106 | +} |
0 commit comments