Get Azure storage blob url after uploading powershell - powershell

How can i get url of the blob i just uploaded using powershell.My code currently is
$storagekey=Get-AzureStorageKey -StorageAccountName appstorageacc
$ctx=New-AzureStorageContext -StorageAccountName
appstorageacc - StorageAccountKey $storagekey.Primary
Set-AzureStorageBlobContent -File C:\Package\StarterSite.zip
-Container clouddata -Context $ctx -Force
Blob is uploaded successfully, but how can i get it's url out?

Retrieve blob information using the Get-AzureStorageBlob cmdlet and select the AbsoluteUri:
(Get-AzureStorageBlob -blob 'StarterSite.zip' -Container 'clouddata ').ICloudBlob.uri.AbsoluteUri

Another option: just capture the result of Set-AzureStorageBlobContent
$result = Set-AzureStorageBlobContent -File C:\Package\StarterSite.zip
$blobUri = $result.ICloudBlob.Uri.AbsoluteUri
It's not necessary to call Get-AzureStorageBlob after calling Set-AzureStorageBlobContent.
Set-AzureStorageBlobContent returns the same object type that Get-AzureStorageBlob returns.
More details
The output type is AzureStorageBlob for both Get-AzureStorageBlob and Set-AzureStorageBlobContent
AzureStorageBlob has a ICloudBlob property
AzureStorageBlob.ICloudBlob getter returns a CloudBlob which has the Uri property

Related

How to rename a blob file using powershell

seemingly simple task. I just want to rename a blob file, I know I have to copy it to rename or something, then delete the original but this is proving tricky. I have created the storage context (New-AzureStorageContext), and got the blob (Get-AzureStorageBlob), and found Start-AzureStorageBlobCopy, but how to I actually rename it?
I'd like to do this within the same container if possible as well. Ideally I'd run it in an Azure Runbook and call it using a webhook I Azure Data Factory v2. I did try to rename the file using 'Add Dynamic Content' in the copy job sink in DFv2, but I don't think you can. By the way, I just want to append the date to the existing file name. Thank you.
You can use my Rename-AzureStorageBlobconvenience function:
function Rename-AzureStorageBlob
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
[Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageBlob]$Blob,
[Parameter(Mandatory=$true, Position=1)]
[string]$NewName
)
Process {
$blobCopyAction = Start-AzureStorageBlobCopy `
-ICloudBlob $Blob.ICloudBlob `
-DestBlob $NewName `
-Context $Blob.Context `
-DestContainer $Blob.ICloudBlob.Container.Name
$status = $blobCopyAction | Get-AzureStorageBlobCopyState
while ($status.Status -ne 'Success')
{
$status = $blobCopyAction | Get-AzureStorageBlobCopyState
Start-Sleep -Milliseconds 50
}
$Blob | Remove-AzureStorageBlob -Force
}
}
It accepts the blob as pipeline input so you can pipe the result of the Get-AzureStorageBlob to it and just provide a new name:
$connectionString= 'DefaultEndpointsProtocol=https;AccountName....'
$storageContext = New-AzureStorageContext -ConnectionString $connectionString
Get-AzureStorageBlob -Container 'MyContainer' -Context $storageContext -Blob 'myBlob.txt'|
Rename-AzureStorageBlob -NewName 'MyNewBlob.txt'
To append the date to the existing file name you can use something like:
Get-AzureStorageBlob -Container 'MyContainer' -Context $storageContext -Blob 'myBlob.txt' | ForEach-Object {
$_ | Rename-AzureStorageBlob -NewName "$($_.Name)$(Get-Date -f "FileDateTime")" }
Further reading: Rename Azure Storage Blob using PowerShell

Issues using Get-StorageBlobContent to downoald blob to local directory

I am trying to use Get-StorageBlobContent to download a blob to local directory. However when I specify the container directory of the blob I get an error. My code looks something like this:
$ctx = New-AzureStorageContext $StorageAccountName -StorageAccountKey $StorageAccountKey
$BlobName = "blob123.vhd"
$LocalTargetDirectory = "D:\vhds"
$ContainerName = transfer/2018/vhds
$Get-AzureStorageBlobContent -Blob $BlobName -Container $ContainerName -Destination $LocalTargetDirectory -Context $ctx
It complains that the container name I specify is not a valid:
Container name 'transfer/2018/vhds' is invalid. Valid names start and end with a lower case letter or a number and has in between a lower case letter, number or dash with no consecutive
My question is then, if the blob I am trying to copy is in a folder in a storage account, how do I correctly give its location?
Any help would be appreciated!
Try something like the following:
$ctx = New-AzureStorageContext $StorageAccountName -StorageAccountKey $StorageAccountKey
$BlobName = "2018/vhds/blob123.vhd"
$LocalTargetDirectory = "D:\vhds"
$ContainerName = "transfer"
$Get-AzureStorageBlobContent -Blob $BlobName -Container $ContainerName -Destination $LocalTargetDirectory -Context $ctx
I am assuming that your blob container name is transfer and blob123.vhd is present in 2018/vhds folder.

Check if table exists in azure storage powershell

i have this powershell using which I want to create new table in azure storage account.
Param(
[string]$rgName,
[string] $tableName
)
$storcontext= New-AzureStorageContext -ConnectionString '$(MyConnectionString)'
if(!(Get-AzureStorageTable -Name $tableName -Context $storcontext ))
{
New-AzureStorageTable -Name $tableName -Context $storcontext
}
New-AzureStorageTable command works perfect.however i tried adding a check to see if table already exists . but on Get command, powershell throws me saying table does not exist.
What I want to do is check if table exist, if not , then create it.
Is there another way of doing this?
The cmdlet throws an error if the table doesn't exists so you could set the ErrorAction to SilentlyContinue and specify a variable for the error which you could check:
Get-AzureStorageTable -Name $tableName -Context $storcontext -ErrorVariable ev -ErrorAction SilentlyContinue
if ($ev) {
New-AzureStorageTable -Name $tableName -Context $storcontext
}

Get-AzureStorageBlob : Could not get the storage context. Please pass in a storage context or set the current storage context

I am using powershell with Azure cmdlets to try and simply see items in blob storage
$StorageContext = New-AzureStorageContext -StorageAccountName 'myblobname' -StorageAccountKey '2341231234asdff2352354345=='
$Container = Get-AzureStorageContainer -Name 'mycontainer' -Context $StorageContext
$blobs = Get-AzureStorageBlob -Container $Container
Error:
Get-AzureStorageBlob : Could not get the storage context. Please pass in a storage context or set the current storage context.
I am 100% sure that the credentials are correct (just random shortened credential data in this post)
Why would I get this error?
Is AzureRM used? The AzureRM version is listed as 3.8.0.
Which versions of which scripts would I need for this to work?
You would need to include StorageContext here as well:
$blobs = Get-AzureStorageBlob -Container $Container
So your code would be:
$blobs = Get-AzureStorageBlob -Container $Container -Context $StorageContext

Upload files and folder into Azure Blob Storage

I have created a storage account in Azure and created a container. I am trying to upload files stored in my Server the files are stored within 800 folders.
I have tried doing this with this Powershell script however it does not work with the subfolders.
$LocalFolder = "\\Server\Data"
Add-AzureAccount
# Set a default Azure subscription.
Select-AzureSubscription -SubscriptionName 'nameofsubscription' –Default
$Key = Get-AzureStorageKey -StorageAccountName mydatastorename
$Context = New-AzureStorageContext -StorageAccountKey $Key.Primary -StorageAccountName mydatastorename
foreach ($folder in Get-ChildItem $LocalFolder)
{
ls –Recurse –Path $LocalFolder |Set-AzureStorageBlobContent -Container nameofcontainer -Context $Context -BlobType Block
}
If set the $LocalFolder as "\Server\Data\subfolders001" the files in subfolder001 get uploaded to the container. But when I keep it as "\Server\Data" then it does not work.
I want the script to upload all the sub folders and files within into the storage container.
I have added the output I get when I run it
I don't get any error message, but one warning message each subfolder
WARNING: Can not upload the directory '\\Server\Data\subfolders001' to azure. If you want to upload directory, please use "ls -File -Recurse | Set-AzureStorageBlobContent -Container containerName".
Then noting happens after waiting for a while I have to stop the powershell script.
Was just solving this myself, see my solution below:
$StorageAccountName = "storageAccountName"
$StorageAccountKey = "StorageAccountKey"
$ContainerName = "ContainerName"
$sourceFileRootDirectory = "AbsolutePathToStartingDirectory" # i.e. D:\Docs
function Upload-FileToAzureStorageContainer {
[cmdletbinding()]
param(
$StorageAccountName,
$StorageAccountKey,
$ContainerName,
$sourceFileRootDirectory,
$Force
)
$ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$container = Get-AzureStorageContainer -Name $ContainerName -Context $ctx
$container.CloudBlobContainer.Uri.AbsoluteUri
if ($container) {
$filesToUpload = Get-ChildItem $sourceFileRootDirectory -Recurse -File
foreach ($x in $filesToUpload) {
$targetPath = ($x.fullname.Substring($sourceFileRootDirectory.Length + 1)).Replace("\", "/")
Write-Verbose "Uploading $("\" + $x.fullname.Substring($sourceFileRootDirectory.Length + 1)) to $($container.CloudBlobContainer.Uri.AbsoluteUri + "/" + $targetPath)"
Set-AzureStorageBlobContent -File $x.fullname -Container $container.Name -Blob $targetPath -Context $ctx -Force:$Force | Out-Null
}
}
}
Running:
Upload-FileToAzureStorageContainer -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey -ContainerName $ContainerName -sourceFileRootDirectory $sourceFileRootDirectory -Verbose
You should see that it prints the following output as it runs:
VERBOSE: Uploading testfile.json to https://storagecontainername.blob.core.windows.net/path/testfile.json
VERBOSE: Performing the operation "Set" on target "/path/testfile.json".
ICloudBlob : Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob
BlobType : BlockBlob
Length : 0
ContentType : application/octet-stream
LastModified : 23/03/2017 14:20:53 +00:00
SnapshotTime :
ContinuationToken :
Context : Microsoft.WindowsAzure.Commands.Common.Storage.AzureStorageContext
Name : /path/testfile.json
VERBOSE: Transfer Summary
--------------------------------
Total: 1.
Successful: 1.
Failed: 0.
I admit this isn't perfect but with a bit of effort you could extend it to cater for more complex scenario file uploads but it should do the trick if you wanted to upload the contents of a directory to a target storage container.
Azure Storage Containers are essentially directories that can't contain other storage containers. So in order to implement a folder structure in an Azure Storage Container, you need to prefix the file with the path up to the root folder you started searching from. I.e. If your root is :
D:\Files
And Files contains:
Folder 1
-> File 1.txt
File 2.txt
You need to set the target paths to be
$StorageContainerObject.CloudBlobContainer.Uri.AbsoluteUri + "\Files\Folder 1\File 1.txt"
$StorageContainerObject.CloudBlobContainer.Uri.AbsoluteUri + "\Files\File 2.txt"
If you view your storage container using a tool like Azure Storage Explorer then it recognises the file structures even though the files are all stored within one container.
Thanks,
Jamie
Azure Blob Storage has only one level of folders - containers.
Instead, you may use few options below
Use Azure File Services
https://learn.microsoft.com/en-us/azure/storage/storage-dotnet-how-to-use-files
Use workaround as described here https://www.codeproject.com/articles/597939/modelingplusaplusdirectoryplusstructureplusonplusa