PowerShell 将 blob 文本上传到 Azure 时出错 : UploadText(string)

标签 powershell azure

我有一个 powershell 模块,它尝试将 blob 上传到 azure 存储。一切都会检查到实际上传 blob 的最后一行。

我收到以下错误:

Exception calling "UploadText" with "1" argument(s): 
"The specified resource does not exist."
At line:1 char:1
    + $blob.UploadText("asdasdfsdf")
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : StorageClientException

我也尝试过使用带有 3 个参数的重载,但也存在同样的问题。

这是模块:

Function Add-BlobText
{
[CmdletBinding()]
param(
      [Parameter(Mandatory = $true,Position = 0)]
      [string]
      $StorageAccount,

      [Parameter(Mandatory = $true,Position = 1)]
      [string]
      $Container,

      [Parameter(Mandatory = $true,Position = 2)]
      [string]
      $BlobName,

      [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
      [string]
      $BlobText
) #end param

Add-Type -Path "C:\Assemblies\Microsoft.WindowsAzure.StorageClient.dll"

Set-AzureSubscription -SubscriptionName "MySubName"

$secondaryKey = (Get-AzureStorageKey -StorageAccountName $storageAccount).Secondary

$creds = New-Object Microsoft.WindowsAzure.StorageCredentialsAccountAndKey($StorageAccount,$secondaryKey)

$cloudStorageAccount = New-Object Microsoft.WindowsAzure.CloudStorageAccount($creds, $true)

[Microsoft.WindowsAzure.StorageClient.CloudBlobClient]$cloudBlobClient = New-Object Microsoft.WindowsAzure.StorageClient.CloudBlobClient($cloudStorageAccount.BlobEndpoint)

[Microsoft.WindowsAzure.StorageClient.CloudBlobContainer]$blobContainer = $cloudBlobClient.GetContainerReference($Container)

[Microsoft.WindowsAzure.StorageClient.CloudBlob]$blob = $blobContainer.GetBlobReference($BlobName)

$blob.UploadText($BlobText)     

} #end Function Add-BlobText

更新:

我已经能够将其作为二进制模块运行(如下)。如果有人能弄清楚为什么 UploadText() 在二进制模块中工作但在脚本模块中抛出异常,请告诉我。

[Cmdlet(VerbsCommon.Add, "BlobText")]
public class AddBlobText : PSCmdlet
{
    [Parameter(Mandatory = true, Position = 0)]
    public string StorageAccount { get; set; }

    [Parameter(Mandatory = true, Position = 1)]
    public string Container { get; set; }

    [Parameter(Mandatory = true, Position = 2)]
    public string BlobName { get; set; }

    [Parameter(Mandatory = true, ValueFromPipeline = true)]
    public string BlobText { get; set; }

    protected override void ProcessRecord()
    {
        PowerShell ps = PowerShell.Create();
        ps.AddScript("Set-AzureSubscription -SubscriptionName 'MySubName'");
        string keyScript = "( Get-AzureStorageKey -StorageAccountName " + StorageAccount + " ).Secondary";
        ps.AddScript(keyScript);
        Collection<PSObject> result = ps.Invoke();
        string secondaryKey = result[0].ToString();
        StorageCredentialsAccountAndKey credentials = new StorageCredentialsAccountAndKey(StorageAccount, secondaryKey);
        CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, true);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference(Container);
        var blob = container.GetBlobReference(BlobName);
        blob.UploadText(BlobText);
    }
}

最佳答案

这可能是因为您的容器不存在。您应该在初始化容器后调用 CreateIfNotExist 以确保它存在:

[Microsoft.WindowsAzure.StorageClient.CloudBlobContainer]$blobContainer = $cloudBlobClient.GetContainerReference($Container)
$blobContainer.CreateIfNotExist() <-- Here
[Microsoft.WindowsAzure.StorageClient.CloudBlob]$blob = $blobContainer.GetBlobReference($BlobName)
$blob.UploadText($BlobText)

关于PowerShell 将 blob 文本上传到 Azure 时出错 : UploadText(string),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12607211/

相关文章:

powershell - 基于 .CSR 文件从 RootCA 生成证书

azure - 使用 Asp .NET Core 的 Multi-Tenancy Web 应用程序登录问题

azure - 什么会阻止 Azure Function Runtime 在 ILB ASE 部署中启动?

powershell - 将 ConvertFrom-SecureString 与 Key 一起使用时出现异常

azure - 在 Bicep 部署中的 AppService 上设置 `customDomainVerificationId` 属性没有效果

azure - 使用 Linux Shell 脚本通过 Azure DevOps 服务 REST API 创建拉取请求

azure - 将 Azure 数据库从一个帐户移动到 Azure 中的另一帐户

powershell - script:在powershell中做什么?

PowerShell 2.0 以及如何处理异常?

powershell - 为什么从 powershell 调用批处理文件时插入符会消失?