You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过PowerShell将SPO库文件迁移至Azure存储容器

解决方案:从SharePoint Online批量迁移文件至Azure Blob存储

前置准备

  • 安装所需PowerShell模块:
    Install-Module -Name PnP.PowerShell -Force -AllowClobber
    Install-Module -Name Az.Storage -Force -AllowClobber
    
  • 确保你具备以下权限:
    • SharePoint Online站点的文件读取权限
    • Azure存储账户的Blob数据参与者或更高权限

完整PowerShell脚本

# 1. 配置核心参数
$csvPath = "C:\path\to\your\files.csv" # 替换为你的CSV文件实际路径
$storageAccountName = "your-storage-account-name" # 替换为你的存储账户名称
$containerName = "your-container-name" # 替换为目标Blob容器名称

# 2. 读取CSV文件内容
$fileList = Import-Csv -Path $csvPath

# 3. 登录SharePoint Online
Connect-PnPOnline -Url $fileList[0].SPOSiteURL -Interactive # 基于CSV首个站点URL登录,多站点需额外处理

# 4. 登录Azure并获取存储上下文
Connect-AzAccount -Interactive
$storageContext = (Get-AzStorageAccount -Name $storageAccountName).Context

# 5. 循环处理每个文件条目
foreach ($item in $fileList) {
    try {
        # 生成临时下载路径(自动避免文件名冲突)
        $tempFile = [System.IO.Path]::GetTempFileName()

        # 从SPO下载目标文件
        Write-Host "正在下载:$($item.FileURL)"
        Get-PnPFile -Url $item.FileURL -Path $tempFile -AsFile -Force

        # 上传至Azure Blob存储(虚拟目录会自动创建)
        Write-Host "正在上传至Blob路径:$($item.AzureBlobLocation)"
        Set-AzStorageBlobContent -File $tempFile `
                                -Container $containerName `
                                -Blob $item.AzureBlobLocation `
                                -Context $storageContext `
                                -Force

        # 清理临时文件
        Remove-Item -Path $tempFile -Force
        Write-Host "文件处理完成:$($item.FileURL)`n"
    }
    catch {
        Write-Error "处理失败 $($item.FileURL):$_"
    }
}

# 断开连接
Disconnect-PnPOnline
Disconnect-AzAccount

关键细节说明

  1. CSV格式要求:确保你的CSV包含以下列名:

    • SPOSiteURL:SharePoint Online站点根URL(例如https://yourtenant.sharepoint.com/sites/yoursite)
    • FileURL:SPO文件的完整访问URL(例如https://yourtenant.sharepoint.com/sites/yoursite/Shared%20Documents/report.pdf)
    • AzureBlobLocation:目标Blob的完整路径(例如reports/2024/report.pdf,虚拟目录无需提前创建)
  2. 多SPO站点适配:如果CSV包含多个不同的SPO站点,需要在循环内添加判断逻辑,当站点URL变化时重新执行Connect-PnPOnline

  3. Blob存储特性:

    • Blob的“目录”是虚拟结构,上传时指定完整路径即可自动生成层级展示
    • -Force参数会覆盖同名Blob,如需保留历史版本可开启存储账户的版本控制功能
  4. 异常处理:脚本包含基础捕获逻辑,单个文件处理失败不会中断整个批量任务,错误信息会直接输出

新手操作提示

  • 首次安装模块时,会提示是否允许从PSGallery安装,按Y确认即可
  • 登录环节会弹出浏览器窗口,使用具备对应权限的账号完成登录
  • 测试阶段建议先在CSV中放入1-2个文件,验证流程正常后再执行批量迁移

内容的提问来源于stack exchange,提问作者user29299371

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 19:50:11