Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zerg00s/e457b0eba2ed39053245886058780dbd to your computer and use it in GitHub Desktop.
Save Zerg00s/e457b0eba2ed39053245886058780dbd to your computer and use it in GitHub Desktop.
Copy
# ================================================================================================== #
# Prerequisites
# ================================================================================================== #
# Install-Module SharePointPnPPowerShellOnline -Scope CurrentUser
# Install-Module -Name Az -Repository PSGallery -Force -Scope CurrentUser
# ================================================================================================== #
# ================================================================================================== #
# Parameters
# ================================================================================================== #
# Azure Storage Account
$storageAccountName = "storageaccname"
$storageAccountKey = "sampleKey61os31UtDZhScbvfGylQlXl3zw8965j2qhrDkhBQqNDRUx2Bz+c+AStSTcL1A=="
$containerName = "containername"
$AzureFolderPath = "Sample Folder A" # Folder within the container that will be used as a destination.
# SharePoint
$SharePointSiteUrl = "https://contoso.sharepoint.com/sites/it-sandbox"
$SharePointFolderUrl = "/sites/it-sandbox/Shared Documents/Sample Folder A"
$SharePointLibraryName = "Documents"
Connect-PnPOnline -Url $SharePointSiteUrl -UseWebLogin
# Get a storage context
$ctx = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
$Container = Get-AzStorageContainer -Name $containerName -Context $ctx
# Function to upload file to Azure Blob Storage using a stream
Function Upload-FileToBlob($fileUrl, $folderPath, $fileName) {
# Get the file content from SharePoint
$fileMemoryStream = Get-PnPFile -Url $fileUrl -AsMemoryStream
# Create the blob path including folder structure
$blobName = "$folderPath/$fileName"
# Upload the stream to Azure Blob Storage
$BlockBlob = $Container.CloudBlobContainer.GetBlockBlobReference($blobName)
$BlockBlob.UploadFromStream($fileMemoryStream);
Write-Host "Uploaded file: $fileUrl to blob: $blobName"
}
# Function to recursively get all folders and files
Function Move-FoldersAndFilesToBlobStorage($SharePointFolderUrl, $AzureFolderPath = "") {
# Retrieve all items in the current folder
$items = Get-PnPListItem -List $SharePointLibraryName -Query "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FileDirRef' /><Value Type='Text'>$SharePointFolderUrl</Value></Eq></Where></Query></View>"
foreach ($item in $items) {
# Check if the item is a folder
if ($item.FileSystemObjectType -eq "Folder") {
# Create new folder path
$newFolderPath = "$AzureFolderPath/$($item.FieldValues.FileLeafRef)"
# Recurse into the folder
Move-FoldersAndFilesToBlobStorage -SharePointFolderUrl $item.FieldValues.FileRef -AzureFolderPath $newFolderPath
} else {
# Upload the file to Azure Blob Storage
Upload-FileToBlob -fileUrl $item.FieldValues.FileRef -folderPath $AzureFolderPath -fileName $item.FieldValues.FileLeafRef
}
}
}
# Start the recursion from the root of the library
Move-FoldersAndFilesToBlobStorage -SharePointFolderUrl $SharePointFolderUrl -AzureFolderPath $AzureFolderPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment