gpt4 book ai didi

azure - 以编程方式上传 Azure Batch 作业应用程序包

转载 作者:行者123 更新时间:2023-12-01 20:08:07 25 4
gpt4 key购买 nike

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

https://learn.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 端点来上传/管理应用程序包?

最佳答案

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

在 Azure 门户中:

  • 创建 Azure Batch 应用程序
  • Create a new Azure AD application (作为应用程序类型,请使用Web应用程序/API)
  • 关注these steps创建 key 并将角色分配给 Azure Batch 帐户
  • 记下以下凭据/ID:
    • Azure AD 应用程序 ID
    • Azure AD 应用程序 key
    • 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);
}

关于azure - 以编程方式上传 Azure Batch 作业应用程序包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42259149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com