简体   繁体   English

如何通过java代码访问和创建azure存储帐户的生命周期规则/生命周期管理策略

[英]How to access and create lifecycle rules/lifecycle management policy for azure storage account through java code

I want to create a lifecycle rule or lifecycle management policy for a specific azure storage account through java code (not through terraform or azure portal).我想通过java 代码(而不是通过 terraform 或 azure 门户)为特定的 azure 存储帐户创建生命周期规则生命周期管理策略 Any appropriate code snippet or reference will be helpful.任何适当的代码片段或参考都会有所帮助。 Thanks in advance.提前致谢。

If you would like to manage the Azure Blob storage lifecycle, you could create it with these following methods.如果要管理 Azure Blob 存储生命周期,可以使用以下方法创建它。

Azure portal, Azure PowerShell, Azure CLI, REST APIs Azure 门户、Azure PowerShell、Azure CLI、REST API

So you could call this REST API to create lifecycle with Java code.因此,您可以调用此 REST API来使用 Java 代码创建生命周期。 You need to get access token, then call the API.您需要获取访问令牌,然后调用 API。 See the sample code, note to change the HTTP request:查看示例代码,注意更改HTTP请求:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM