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

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

我想通过 java 代码(而不是通过 terraform 或 azure)为特定 azure 存储帐户创建生命周期规则生命周期管理策略门户网站)。任何适当的代码片段或引用都会有所帮助。提前致谢。

最佳答案

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

Azure portal, Azure PowerShell, Azure CLI, REST APIs

所以你可以调用this REST API使用 Java 代码创建生命周期。您需要获取访问 token ,然后调用 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;
    }
}

关于java - 如何通过java代码访问和创建azure存储帐户的生命周期规则/生命周期管理策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64458371/

相关文章:

php - 在php中获取azure存储中的Blob目录名称

azure - 当前正在此存储帐户上执行需要独占访问的操作

java - 我必须如何/在何处/何时关闭 MongoClient?

java - JFileChooser 不会死?

java - 日历,传递 ArrayList<HashMap,并迭代它

azure - 如何使用 DocumentDB 集合中的纬度和经度查找最近的点?

java - 在 Activity 而不是 Fragments 中设置 RetainInstance

azure - Azure DevOps 管道中的条件阶段执行

azure - 是否可以在 Azure DevOps 部署期间管理 Verizon Premium CDN 的规则引擎?

rest - 交付和使用 Azure REST-API SharedKey 进行 Blob 上传