【问题标题】:How to access and create lifecycle rules/lifecycle management policy for azure storage account through java code如何通过 java 代码访问和创建 Azure 存储帐户的生命周期规则/生命周期管理策略
【发布时间】:2021-02-04 01:28:20
【问题描述】:

我想通过 java 代码(而不是通过 terraform 或 azure门户网站)。任何适当的代码 sn-p 或参考都会有所帮助。提前致谢。

【问题讨论】:

    标签: java azure azure-storage azure-blob-storage blobstorage


    【解决方案1】:

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

    Azure 门户、Azure PowerShell、Azure CLI、REST API

    因此您可以调用this REST API 来使用Java 代码创建生命周期。您需要获取访问令牌,然后调用 API。见示例代码,注意更改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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-25
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 2023-01-02
      • 2021-07-23
      相关资源
      最近更新 更多