本文介紹了如何通過Java代碼訪問和創(chuàng)建Azure存儲(chǔ)賬戶的生命周期規(guī)則/生命周期管理策略的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我要通過java代碼(而不是通過Terraform或Azure門戶)為特定的Azure存儲(chǔ)帳戶創(chuàng)建生命周期規(guī)則或生命周期管理策略。任何適當(dāng)?shù)拇a片段或引用都會(huì)很有幫助。提前謝謝。
推薦答案
如果要管理Azure Blob存儲(chǔ)生命周期,可以使用以下方法創(chuàng)建它。
Azure門戶、Azure PowerShell、Azure命令行界面、睡覺API
這樣您就可以調(diào)用this REST API來使用Java代碼創(chuàng)建生命周期。您需要獲取訪問令牌,然后調(diào)用接口。請(qǐng)參閱示例代碼,注意更改HTTP請(qǐng)求:
public class PublicClient {
/*tenant_id can be found from your azure portal. Login into azure portal and browse to active directory and choose the directory you want to use. Then click on Applications tab and at the bottom you should see "View EndPoints". In the endpoints, the tenant_id will show up like this in the endpoint url's: https://login.microsoftonline.com/{tenant_id} */
private final static String AUTHORITY = "https://login.microsoftonline.com/{tenant_id}";
public static void main(String args[]) throws Exception {
AuthenticationResult result = getAccessTokenFromUserCredentials();
System.out.println("Access Token - " + result.getAccessToken());
HttpClient client = new DefaultHttpClient();
/* replace {subscription_id} with your subscription id and {resourcegroupname} with the resource group name for which you want to list the VM's. */
HttpGet request = new HttpGet("https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resourcegroupname}/providers/Microsoft.ClassicCompute/virtualMachines?api-version=2014-06-01");
request.addHeader("Authorization","Bearer " + result.getAccessToken());
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null)
{
System.out.println(line);
}
}
private static AuthenticationResult getAccessTokenFromUserCredentials() throws Exception {
AuthenticationContext context = null;
AuthenticationResult result = null;
ExecutorService service = null;
try {
service = Executors.newFixedThreadPool(1);
context = new AuthenticationContext(AUTHORITY, false, service);
/* Replace {client_id} with ApplicationID and {password} with password that were used to create Service Principal above. */
ClientCredential credential = new ClientCredential("{client_id}","{password}");
Future<AuthenticationResult> future = context.acquireToken("https://management.azure.com/", credential, null);
result = future.get();
} finally {
service.shutdown();
}
if (result == null) {
throw new ServiceUnavailableException("authentication result was null");
}
return result;
}
}
這篇關(guān)于如何通過Java代碼訪問和創(chuàng)建Azure存儲(chǔ)賬戶的生命周期規(guī)則/生命周期管理策略的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,