Search code examples
azureazure-devopsazure-storageazure-blob-storage

Set-AzureStorageBlobContent places all files in root


I'm using the Set-AzureStorageBlobContent to upload files to my blob:

foreach ($file in Get-ChildItem -Path $path -File -Recurse) {  
  Set-AzureStorageBlobContent 
    -Blob $file.Name 
    -Container $container 
    -File $file.FullName 
    -Context $context -Properties $prop -Force
}

This works but all the files in subfolders in the $path folder are placed in the root folder of the storage blob.

For example in my $path directory (the source files) I have a folder called graphic inside that folder I have my images image1.png so the structure is:

graphic
  image1.png
index.html

When I run the Set-AzureStorageBlobContent task it uploads the image1.png into the root folder instead of creating a graphic folder and placing it in there.

Is it possible to keep the folder structure when uploading files using Set-AzureStorageBlobContent?


Solution

  • Actually, I have mentioned it in my previous answer.

    This is due to the -Blob $file.Name, for example, if the file a.txt is located at d:\temp\sub\a.txt, then the $file.name is a.txt. So when upload to blob statorage with the cmdlet Set-AzureStorageBlobContent, it always in the root folder.

    The solution is that you should pass sub\a.txt instead of a.txt for the -Blob parameter.

    First, define a variable for the root path of local folder, like $path = "D:\temp\", then for the -Blob parameter for Set-AzureStorageBlobContent, use -Blob $_.FullName.Replace($path,'').

    Then you can check it on azure portal, the file a.txt should be in root/sub/a.txt