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

huangapple go评论79阅读模式
英文:

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

问题

我想通过Java代码(而非通过Terraform或Azure门户)为特定的Azure存储帐户创建一个lifecycle rulelifecycle management policy。任何适用的代码片段或参考资料都将非常有帮助。提前致谢。

英文:

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). Any appropriate code snippet or reference will be helpful. Thanks in advance.

答案1

得分: 2

以下是翻译好的部分:

如果您想管理Azure Blob存储的生命周期,您可以使用以下方法进行创建。

> Azure 门户,Azure PowerShell,Azure CLI,REST API

因此,您可以使用此REST API通过Java代码创建生命周期。您需要获取访问令牌,然后调用该API。请参阅示例代码,注意更改HTTP请求:

public class PublicClient {

    /*tenant_id可以从您的Azure门户中找到。登录Azure门户,浏览到活动目录,选择要使用的目录。然后点击"应用程序"选项卡,在底部您应该会看到"查看终结点"。在终结点中,tenant_id将显示在终结点URL中,类似于: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();

        /* 使用您的订阅ID替换{subscription_id},使用您要列出VM的资源组名称替换{resourcegroupname}。 */

        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);
            /* 使用创建服务主体时使用的ApplicationID替换{client_id},使用密码替换{password}。 */
            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;
    }
}
英文:

If you would like to manage the Azure Blob storage lifecycle, you could create it with these following methods.

> Azure portal, Azure PowerShell, Azure CLI, REST APIs

So you could call this REST API to create lifecycle with Java code. You need to get access token, then call the API. See the sample code, note to change the HTTP request:

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 &quot;View EndPoints&quot;. In the endpoints, the tenant_id will show up like this in the endpoint url&#39;s: https://login.microsoftonline.com/{tenant_id} */
    private final static String AUTHORITY = &quot;https://login.microsoftonline.com/{tenant_id}&quot;;

    public static void main(String args[]) throws Exception {

        AuthenticationResult result = getAccessTokenFromUserCredentials();
        System.out.println(&quot;Access Token - &quot; + 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&#39;s. */

        HttpGet request = new HttpGet(&quot;https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resourcegroupname}/providers/Microsoft.ClassicCompute/virtualMachines?api-version=2014-06-01&quot;);
        request.addHeader(&quot;Authorization&quot;,&quot;Bearer &quot; + result.getAccessToken());
        HttpResponse response = client.execute(request);
        BufferedReader rd = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
        String line = &quot;&quot;;
        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(&quot;{client_id}&quot;,&quot;{password}&quot;);
            Future&lt;AuthenticationResult&gt; future = context.acquireToken(&quot;https://management.azure.com/&quot;, credential, null);
            result = future.get();
        } finally {
            service.shutdown();
        }
        if (result == null) {
            throw new ServiceUnavailableException(&quot;authentication result was null&quot;);
        }
        return result;
    }
}

huangapple
  • 本文由 发表于 2020年10月21日 15:10:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64458371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定