PowerShell Script to Migrate Documents from Azure Blob Storage to SharePoint Library

I was thrown an interesting challenge recently. Take a large set of documents from Azure Blob Storage and move them into a SharePoint Library. I needed to move a large number of documents and tag them with metadata as part of the migration. This is a good job for PowerShell.

I wrote a script that reads a CSV file and then does these steps for each file in the file:

  • Downloads the file from Azure Blob Storage to the local disk
  • Uploads the local file to SharePoint and tags with metadata.

You need to install the Azure PowerShell Module and PnP PowerShell if you don’t have them already.

Here is a sample script, using PnP PowerShell to Connect to SharePoint. Run this with PowerShell 7

#Authenticate Source Site
connect-PnPOnline -url https://tenantname.sharepoint.com/sites/SiteName -Interactive

#AzureTenantDetails

$AzureSubcription = "87654321-1a2b-17aa-123c-12345abcd123"
$Container ="MyContainername"
$BlobPath ="MyContainer/Folderpath/"


#Authenticate for the Azure Account
Connect-AzAccount -Subscription $AzureSubcription

$Context = New-AzStorageContext -StorageAccountName olhenchmansynctest
$Blobs = get-AZStorageBlob -Container $Container

# Read CSV file
$files = Import-CSV C:\temp\IMPORT.csv -Header DocumentName, Filename, StorePath, ClientNumber,ClientName,MatterNumber,AuthorInitials,AuthorName

# for each line in the CSV file download from BLOB Store and Upload to SharePoint
foreach($file in $files)
{

  $SPFolderPath = $file.storepath
  $BlobFilePath = $BlobPath + $file.StorePath + "/" + $file.filename

 #Get Blob  - each file is stored as a blob
  $Blob = @{
  Blob        = $BlobFilePath
  Container   = $Container
  Destination = 'c:\temp\'
  Context     = $Context
  }
  Get-AzStorageBlobContent @Blob -Force
  
  #Upload Blob to SharePoint
  $LocalFilePath = "C:\Temp\foldername\" + $file.ClientNumber + "\" + $file.MatterNumber + "\" + $file.filename
  #Create folder in SharePoint if it doesn't exist
  Resolve-PnPFolder -SiteRelativePath $SPFolderPath
  #upload file from local disk to SharePoint Online
  Add-PnPFile -Path $LocalFilePath -Folder $SPFolderPath -Values @{ClientName=$file.ClientName;AuthorInitials=$file.AuthorInitials;AuthorName=$file.AuthorName}
 }

}

I few things I learnt building the script:

Connecting to Azure Blob Storage required me to understand how content was structured. In this case, there was 1 container with a folder path to the Blobs (a file is a blog). I needed the GUID for the Azure subscription and a CSV file listing each file and the metadata associated with it. The metadata in this case was sourced from a system that uses Azure Blob store to hold documents and was extracted from a related SQL database.

You can authenticate with Azure using users accounts or a service principal. See AzConnect for more details.

I had some trouble with the Blob path initially. I discovered that it doesn’t have a leading “/” character (see $Blobs) in the script above.

The script creates a folder path on the local PC it is running on, following the same path that is configured in the Blob storage.

I hope this script helps you figure this out for your own migration tasks.

Leave a comment