Get-AzureStorageBlobContent to variable (Feature Request)
Cmdlet(s)
Get-AzureStorageBlobContent
PowerShell Version
Instructions: to get PowerShell version, type $PSVersionTable and look for the value associated with PSVersion
all
Module Version
all
OS Version
Instructions: to get OS version, type $PSversionTable and look for value associated with BuildVersion
all
Description
Currently, you can only use this command to download blob content to a file. It would be beneficial if functionality could be added to save blob content to a variable instead of to a local file, for cases where you may not have access to the local file system or may only want the data to be in memory and not to disk.
Debug Output
Instructions: to get Debug Output, set $DebugPreference="Continue" and then execute the cmdlet or script causing the issue
Script/Steps for Reproduction
N/A, new feature request
@blueww would you mind taking a look at this feature request?
@jboeshart Would you please share more details for your scenario, such as how you will use the data in memory, which cmdelts you will use to handle the variable contains the content. It will help us better understand your requirement. Thanks!
Sorry for the delay, missed your comment here @blueww. In my case I wanted to get JSON content straight out of an NSG Flow Log that is sitting on Azure Storage, parse through the JSON and then upload it to OMS. Presently, I have to send that blob content to a local file, read it back in and then do the parsing. If I could get that blob content straight to a variable it would save the step of having to write to a file and read it back in, and could help in cases where I may not have access to or may not want to write to a local filesystem (Azure Functions, Azure Automation, etc.). Ideally I'd like to be able to do something like this:
$blobContent = Get-AzureStorageBlobContent -Container $ContainerName -Context $storageContext -Blob $blob.Name
@jboeshart We have opened a task to track the issue and will triage it according to our bandwidth.
Please note, as the blob content might be very big, we will consider to transfer it to a stream (e.g. memory stream) instead of put the text to a variable
Thanks @blueww. I'm not familiar with using a stream, how does that impact the usability of the data that is in the blob?
You can new an System.IO.MemoryStream object in PowerShell, and we storage cmdlet will download blob content to the memory stream, and you can read from the memory steam. You can see some sample to use memory stream in PowerShell in : https://social.technet.microsoft.com/Forums/windowsserver/en-US/7d838d53-bfd5-4706-b0df-c4bee2d00412/using-memorystream-and-textreader?forum=winserverpowershell
I'm facing the same scenario where I use the blob store with lots of JSON documents that I read and take actions on. It being implemented in azure functions and would like to have the ability to read in memory. If I may - it would be nice to use e.g. a switch like -AsString that will pass on the contents of the blob as a string and thus can pipe it to convertfrom-json directly and thus do e.g. like this
$blobs | Get-AzureStorageBlobContent -AsString | ConvertFrom-Json ...
I understand that blobs may be really BIG but in my case they are rather small and should fit in memory just fine :)
Cheers, Mario
@mariotoffia As you know, PowerShell is a common tool, so we need to consider many facts when convert blob content to string, like big blob, string encoding ...
I will add your comments to the track item for this issue. Before we get a solution on this, can you use:
Get-AzureStorageBlobContent -Destination $filename
get-content $filename | ConvertFrom-Json
@blueww Yes, that exactly what I've done - a custom commandlet for this :)
I understand that you've targeting a broad audience - but if you use a flag that indicates a In-Memory (maybe with a constraint of maximum 4096/8192 bytes or so) I don't see the problem. Or will this drive support issues due to people don't read the help text?
It actually would ease the load on azure-functions on Microsoft server farms due to no IO on disc and (possibly) network (if network attached storage) for storing temporary files :)
Cheers, Mario
My custom hack (not in memory though):
$file = New-TemporaryFile
$blob = Get-AzureStorageBlobContent -Container $name -Blob $f -Context $AuthCtx.StorageContext -Destination $file -Force
$contents = Get-Content $file -Raw -ErrorAction:SilentlyContinue
Remove-Item $file -Force -ErrorAction:SilentlyContinue
$contents
@blueww How does reading the blob content straight to memory differ from writing it to a file then reading it back in if we're talking about a large blob file? Wouldn't the same concerns around memory apply here, just that you're pushing the problem to Get-Content instead of Get-AzureStorageBlobContent?
I need to play around with writing to a memory stream to see if that works. It does add some code smell though when talking pure PowerShell, as it's not as intuitive a process as leveraging a cmdlet parameter.
@mariotoffia thanks for that. it even works from an azure runbook which was my particular problem :)
As a follow up, I've not been able to get the stream object figured out, but you can download blob content directly to a variable using REST calls with a URI containing a SAS token (or to storage accounts that have blob or container access policy). Would still be nice to be able to do this with the Get-AzureStorageBlobContent cmdlet though.
$storageContext = New-AzureStorageContext -StorageAccountName $StorageAccountName -SasToken $SasToken
$blobs = Get-AzureStorageBlob -Container $ContainerName -Context $storageContext
foreach ($blob in $blobs) {
$blobcontent = Invoke-RestMethod -Uri $($blob.ICloudBlob.Uri.ToString() + $SasToken)
}
Thanks @jboeshart and @mariotoffia for the script to resolve the problem!
As request to read small blob content to a local variable can be done with the script provided by @jboeshart , I will close this issue. If you have more request or see other issues, please open a new issue.
That's a very dumb excuse for closing a feature request. I guess, since we can do EVERYTHING using REST API calls, we should also get rid of ALL azure PowerShell cmdlets, no? After all, we don't need them, we have REST API calls... Sure, it's a neat workaround, but we're forced to mix cmdlets with REST API calls and in my first contact with this cmdlet I WAS NOT expecting it to DOWNLOAD to a file! I was trying to GET the blob content (isn't that the cmdlet name? Get-AzureStorageBlobContent ?) I was expecting it to behave the same as the more well known Get-Content cmdlet. MS is not being very coherent here...
I have to agree with @fmad here as well @blueww, can we get this enhancement so that it works more like Get-Content? From a usability standpoint, I think there is a good case for this.
@fmad, @jboeshart, the task to track the change request still open. There are 2 possible fix, which one do you like?
-
Output the blob content to a stream. (see how to use memory stream in link). This can be apply to big blob.
-
Output the blob content as string (will add a new paratmeter like -AsString), this can only apply to small blob.
- We will set a limitation for the blob size (e.g. 100MB). When blob size bigger than the limitation, the cmdlet will fail, and no content will be get.
- Use string as output. Do you need it as byte array or enable encoding Type?
Which one do you prefer?
I discovered that there is actually the ability to grab the file directly into a variable it's just a little hidden:
$StorageAccount = Get-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName
$Blob = Get-AzureStorageBlob -Context $StorageAccount.Context -Container $container -Blob $Filename
$Text = $blob.ICloudBlob.DownloadText()
Write-Output $Text
Having a look at the different download options ($Blob.ICloudBlob | Get-Member | where {$_.Name -like "Download*"} | select Name) gives you:
DownloadBlockList
DownloadBlockListAsync
DownloadRangeToByteArray
DownloadRangeToByteArrayAsync DownloadRangeToStream
DownloadRangeToStreamAsync
DownloadText
DownloadTextAsync
DownloadToByteArray
DownloadToByteArrayAsync
DownloadToFile
DownloadToFileAsync
DownloadToStream
DownloadToStreamAsync
As you can see there are options for Text, Byte, File and Stream.
My personal opinion is that the Get-AzureStorageBlobContent should get the content by default as text but have multiple switches for Stream, Byte and File but we could minimize backward compatibility issues by defaulting to the -File switch which maintains current behavior.
@ld0614 Thanks for you suggestion! We will consider to Support File/Text/Byte Array/Stream in the Get-AzureStorageBlobContent. But for Text / Byte Array, if we don't limit the blob size, OOM issue will happen for big blob. So we plan to add limitation for blob size (e.g. 100MB) if output is text/Byte Array. For Stream, user should create the stream and input to the cmdlet, or there will also be OOM issue. @fmad, @jboeshart, @ld0614 Any comments?
I agree that it should be text by default and have other options for other download formats. I think the 100MB limitation is acceptable as well to start. I would guess that if you've got a larger blob it's more efficient to download locally or leverage another mechanism to process the data. If there's a valid use case to make that number bigger down the line it could be addressed at that point via another enhancement, but this would give a good starting point. Curious how that would be handled though. Would the blob size be evaluated just prior to download and an error thrown if it's >100MB? We'd also need to ensure this is clearly noted in our documentation.
Leveraging the .ICloudBlob.DownloadText() method that @ld0614 showed is a cool way to do that in the interim, thanks for sharing that! This could also be used in future edge cases where the blob is >100MB.
I must admit that I'm not normally a fan of arbitrary limits to functionality. My first thought is that does the ICloudBlob already implement similar functionality and if so what are the limits?
While I fully accept that cmdlets should be designed in a way that doesn't kill the environment by default I believe that the ability to modify the default behavior could come in very handy and shouldn't have to resort to workarounds or hacks.
I think that it will be very important that this cmdlet doesn't lock up during the download process, this would allow users who are actively monitoring the script/command to stop the process without resorting to closing the PowerShell environment.
As such my proposals are: Check when a file is over 100MB and write a warning to the screen warning, this wouldn't change the behavior but would explain the massive memory use to the user and combined with the ability to stop mentioned above should stop the majority of the failures due to inexperience. This warning can then be silenced if required or taken more seriously by modification of WarnAction
Introduce an additional parameter for maxFileSize. This would be optional but default to 100MB but could be modified to any limit (ideally with a * or something option for bypass). This would allow for the behavior mentioned in other posts but would allow for overriding when a user has the requirement to.
My view is that if the user has visual feedback and can change their mind and stop it or has to active set something to override behavior then that is then their fault if they cause an environment to fail. If they do inadvertently cause a failure then an investigation should be able to show the reason either via warnings or verbose logging and a quick read of the Microsoft cmdlet documentation should also mention that data is pulled into memory. This then allows the user to write intelligent code which meets their requirements.