use of com.aliyuncs.auth.InstanceProfileCredentialsProvider in project fc-java-sdk by aliyun.
the class FunctionComputeClientTest method testClientCredentialProvider.
/**
* run only on aliyun ecs, and that ecs need bind a RAM Role
*/
public void testClientCredentialProvider() {
// init CredentialProvider
String roleName = "ECSAccessingFCTestRole";
InstanceProfileCredentialsProvider credsProvider = new InstanceProfileCredentialsProvider(roleName);
// init fc client
Config config = new Config(REGION, ACCOUNT_ID, credsProvider, false);
FunctionComputeClient fcClient = new FunctionComputeClient(config);
client = fcClient;
// Create a service
try {
createService(SERVICE_NAME, false);
} catch (Exception e) {
e.printStackTrace();
}
// create a function
try {
createFunction(FUNCTION_NAME);
} catch (Exception e) {
e.printStackTrace();
}
// Invoke the function with a string as function event parameter, Sync mode
InvokeFunctionRequest invkReq = new InvokeFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
String payload = "Hello FunctionCompute!";
invkReq.setPayload(payload.getBytes());
InvokeFunctionResponse invkResp = client.invokeFunction(invkReq);
System.out.println(new String(invkResp.getContent()));
cleanUpFunctions(SERVICE_NAME);
cleanupService(SERVICE_NAME);
}
use of com.aliyuncs.auth.InstanceProfileCredentialsProvider in project fc-java-sdk by aliyun.
the class FunctionComputeClientTest method testClientCredentialProviderMock.
@Test
public void testClientCredentialProviderMock() {
// init CredentialProvider
String ak = "ak";
String sk = "sk";
String stsToken = "sts_token";
BasicSessionCredentials creds = new BasicSessionCredentials(ak, sk, stsToken);
// mock
InstanceProfileCredentialsProvider credsProvider = mock(InstanceProfileCredentialsProvider.class);
try {
when(credsProvider.getCredentials()).thenReturn(creds);
} catch (com.aliyuncs.exceptions.ClientException e) {
e.printStackTrace();
}
// init fc client
Config config = new Config(REGION, ACCOUNT_ID, credsProvider, false);
FunctionComputeClient fcClient = new FunctionComputeClient(config);
client = fcClient;
// Create a service
try {
createService(SERVICE_NAME);
} catch (Exception e) {
}
assertEquals(creds.getAccessKeyId(), config.getAccessKeyID());
assertEquals(creds.getAccessKeySecret(), config.getAccessKeySecret());
assertEquals(creds.getSessionToken(), config.getSecurityToken());
}
Aggregations