Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过java代码访问和创建azure存储帐户的生命周期规则/生命周期管理策略_Java_Azure_Azure Storage_Azure Storage Blobs_Blobstorage - Fatal编程技术网

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

如何通过java代码访问和创建azure存储帐户的生命周期规则/生命周期管理策略,java,azure,azure-storage,azure-storage-blobs,blobstorage,Java,Azure,Azure Storage,Azure Storage Blobs,Blobstorage,我想通过java代码(而不是通过terraform或azure portal)为特定azure存储帐户创建生命周期规则或生命周期管理策略。任何适当的代码片段或参考都会有所帮助。提前感谢。如果您想管理Azure Blob存储生命周期,可以使用以下方法创建它 Azure门户、Azure PowerShell、Azure CLI、REST API 因此,您可以调用Java代码来创建生命周期。您需要获取访问令牌,然后调用API。请参阅示例代码,注意更改HTTP请求: public class Publi

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

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

Azure门户、Azure PowerShell、Azure CLI、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;
    }
}
公共类PublicClient{
/*可以从您的azure门户中找到租户id。登录azure门户并浏览到active directory并选择要使用的目录。然后单击“应用程序”选项卡,在底部您将看到“查看端点”。在端点中,租户id将显示在端点url中,如下所示:https://login.microsoftonline.com/{租户_id}*/
私有最终静态字符串权限=”https://login.microsoftonline.com/{租户_id}”;
公共静态void main(字符串args[])引发异常{
AuthenticationResult=getAccessTokenFromUserCredentials();
System.out.println(“访问令牌-”+result.getAccessToken());
HttpClient=new DefaultHttpClient();
/*将{subscription_id}替换为您的订阅id,{resourcegroupname}替换为您要为其列出VM的资源组名称*/
HttpGet请求=新建HttpGet(“https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resourcegroupname}/providers/Microsoft.classiccomputer/virtualMachines?api version=2014-06-01”);
request.addHeader(“授权”、“承载者”+result.getAccessToken());
HttpResponse response=client.execute(请求);
BufferedReader rd=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
字符串行=”;
而((line=rd.readLine())!=null)
{
系统输出打印项次(行);
}
}
私有静态身份验证结果getAccessTokenFromUserCredentials()引发异常{
AuthenticationContext上下文=null;
AuthenticationResult=null;
ExecutorService=null;
试一试{
服务=Executors.newFixedThreadPool(1);
context=新的AuthenticationContext(AUTHORITY、false、service);
/*将{client_id}替换为ApplicationID,{password}替换为用于创建上述服务主体的密码*/
ClientCredential=newclientcredential(“{client_id},“{password}”);
Future=context.acquireToken(“https://management.azure.com/“,凭证,空);
结果=future.get();
}最后{
service.shutdown();
}
如果(结果==null){
抛出新的ServiceUnavailableException(“身份验证结果为空”);
}
返回结果;
}
}