PowerShell error loading blob text in Azure: UploadText (string)

I have a powershell module that is trying to load blob into azure storage. Everything is checked to the last line, which actually loads blob.

I get the following error:

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 

I also tried using overload with 3 arguments, but the same problem exists there too.

Here is the module:

 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 

Update:

I managed to get this working as a binary module (see below). If someone can understand why UploadText() works in a binary module but throws an exception in a script module, let me know.

 [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); } } 
+4
source share
3 answers

This is probably because your container does not exist. You must call CreateIfNotExist after initializing the container to make sure it exists:

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

This error is very ambiguous and misleading, but there are times when "Azure Storage" can get confused. Considering the example of Sandrino and, in particular, this line,

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

Not that Sandrino's answer is your problem, but the exception you encounter will happen when passing the URL or possibly other confusing key lines to Azure Storage Containers.

Unfortunately, I'm not a Powershell guy, but here is a playback example, and then fix it in C #.

  public void Save(string objId, T obj) { CloudBlob blob = this.container.GetBlobReference(objId); // Problematic if a URL blob.Properties.ContentType = "application/json"; var serialized = string.Empty; serialized = serializer.Serialize(obj); if (this.jsonpSupport) { serialized = this.container.Name + "Callback(" + serialized + ")"; } blob.UploadText(serialized); } 

Suppose this.container is a valid blob storage instance pointing to http://127.0.0.1:10000/devstoreaccount1/sggames or whatever you have for a valid container.

And objId is the key that contains the url, for example https://www.google.com/accounts/o8/id?id=AItOawk4Dw9sLxSc-zmdWQHdZNcyzkTcvKUkhiE ... and yes, this can happen, in my case this is the actual claim from Google using Azure ACS.

After calling GetBlobReference, the blob instance became corrupt, which now looks confused Uri → https://www.google.com/accounts/o8/id?id=AItOawk4Dw9sLxSc-zmdWQHdZNcyzkTcvKUkhiE

Unfortunately, the solution to just calling $ blobContainer.CreateIfNotExist () is not a solution and will not work. The key containing the Uri structure will simply be used to re-interpret the blob storage location.

The work around (except the daredev update) will be something like this:

 if (Uri.IsWellFormedUriString(claim, UriKind.Absolute) && HttpUtility.ParseQueryString(claim).Count > 0) { claim = HttpUtility.ParseQueryString(claim)[0]; } 

Add this code to my method above to clear all Uri, but you can use any suitable method, such as Base64 encoding URLs, if you need to save the full key.

Here are the before and after images showing the results as I described.

Bad:

notice the bad URI note the bad URI

this bad URI munged up the actual storage blob location this bad URI mixed up the actual location of the memory location

here is the same exception dardev had here is the same exception daredev had

Good:

the new scrubbed key, notice it's just the value on the URL's query string new cleared key, pay attention only to the value in the query string URL

Azure Storage URI looks good now Laser storage URI now looks good

Eureka! Eureka!

Hope this helps.

+2
source

This is a PowerShell script I use to upload a file in Azure Blob: Upload to Azure Storage

 $SubscriptionName = "" $SubscriptionId = "" $DestContainer = "" $StorageAccountName = "" Import-AzurePublishSettingsFile -PublishSettingsFile "<Location of the publishsettings-file>" Set-AzureSubscription -SubscriptionId $SubscriptionId -CurrentStorageAccountName $StorageAccountName Select-AzureSubscription -SubscriptionName $SubscriptionName Set-AzureStorageBlobContent -File "<File you want to upload>" -Container $DestContainer 
+1
source

Source: https://habr.com/ru/post/1436431/


All Articles