3

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

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 放在一起。在设置批处理作业时,我们是否可以调用 API 端点来上传/管理应用程序包?

4

2 回答 2

9

由于这不是很简单,我将写下我的发现。这些是通过无人值守的应用程序以编程方式上传应用程序包的步骤 - 不需要用户输入(例如 Azure 凭据)。

在 Azure 门户中:

在您的代码中:

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

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);
}
于 2017-10-25T08:02:46.433 回答
1

Azure Batch 应用程序包管理操作发生在管理平面上。这个命名空间的 MSDN 文档在这里:

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

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

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

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

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

于 2017-02-15T23:04:02.127 回答