以编程方式上载Azure Batch Job Application Package

时间:2017-02-15 20:14:39

标签: azure azure-batch

我已经找到了如何通过UI上传/管理Azure批处理作业应用程序包:

https://docs.microsoft.com/en-us/azure/batch/batch-application-packages

如何以编程方式上传和管理资源包:

https://github.com/Azure/azure-batch-samples/tree/master/CSharp/GettingStarted/02_PoolsAndResourceFiles

但我似乎无法将2和2放在如何以编程方式管理Application Packages。在设置批处理作业时,是否有可以调用上传/管理应用程序包的API端点?

2 个答案:

答案 0 :(得分:6)

由于这不是那么简单,我会写下我的发现。 这些是通过无人参与的应用程序以编程方式上载应用程序包的步骤 - 无需用户输入(例如Azure凭据)。

在Azure门户中:

  • 创建Azure批处理应用程序
  • Create a new Azure AD application(作为应用类型使用Web app / API
  • 按照these steps创建密钥并将角色分配给Azure批处理帐户
  • 记下以下凭据/ ID:
    • Azure AD应用程序ID
    • Azure AD应用程序密钥
    • Azure AD tenant id
    • Subscription id
    • 批量帐户名称
    • 批量帐户资源组名称

在您的代码中:

将整个代码放在一起看起来像这样:

private const string ResourceUri = "https://management.core.windows.net/";
private const string AuthUri = "https://login.microsoftonline.com/" + "{TenantId}";
private const string ApplicationId = "{ApplicationId}";
private const string ApplicationSecretKey = "{ApplicationSecretKey}";
private const string SubscriptionId = "{SubscriptionId}";
private const string ResourceGroupName = "{ResourceGroupName}";
private const string BatchAccountName = "{BatchAccountName}";

private async Task UploadApplicationPackageAsync() {
    // get the access token
    var authContext = new AuthenticationContext(AuthUri);
    var authResult = await authContext.AcquireTokenAsync(ResourceUri, new ClientCredential(ApplicationId, ApplicationSecretKey)).ConfigureAwait(false);

    // create the BatchManagementClient and set the subscription id
    var bmc = new BatchManagementClient(new TokenCredentials(authResult.AccessToken)) {
        SubscriptionId = SubscriptionId
    };

    // create the application package
    var createResult = await bmc.ApplicationPackage.CreateWithHttpMessagesAsync(ResourceGroupName, BatchAccountName, "MyPackage", "1.0").ConfigureAwait(false);

    // upload the package to the blob storage
    var cloudBlockBlob = new CloudBlockBlob(new Uri(createResult.Body.StorageUrl));
    cloudBlockBlob.Properties.ContentType = "application/x-zip-compressed";
    await cloudBlockBlob.UploadFromFileAsync("myZip.zip").ConfigureAwait(false);

    // create the application package
    var activateResult = await bmc.ApplicationPackage.ActivateWithHttpMessagesAsync(ResourceGroupName, BatchAccountName, "MyPackage", "1.0", "zip").ConfigureAwait(false);
}

答案 1 :(得分:1)

Azure批处理应用程序包管理操作在管理平面上进行。此命名空间的MSDN文档位于:

https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.management.batch

Microsoft.Azure.Management.Batch的nuget包在这里:

https://www.nuget.org/packages/Microsoft.Azure.Management.Batch/

以下示例显示了C#中的管理平面操作,尽管它适用于非应用程序包操作:

https://github.com/Azure/azure-batch-samples/tree/master/CSharp/AccountManagement