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

Lan*_*sby 3 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放在如何以编程方式管理应用程序包.在设置批处理作业时,是否有可以调用上传/管理应用程序包的API端点?

Rob*_*bar 8

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

在Azure门户中:

  • 创建Azure批处理应用程序
  • 创建新的Azure AD应用程序(作为应用程序类型使用Web app / API)
  • 按照以下步骤创建密钥并将角色分配给Azure批处理帐户
  • 记下以下凭据/ ids:
    • Azure AD应用程序ID
    • Azure AD应用程序密钥
    • Azure AD 租户ID
    • 订阅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);
}
Run Code Online (Sandbox Code Playgroud)