Powershell:Azcopy一个文件夹结构从本地驱动到blob存储

问题描述

您好,我是 azure 门户和 powershell 的新手。

我正在尝试将本地驱动器复制到 blob 存储,同时保持文件夹结构。此外,如果有一个清单文件,它也会为我提供一个文件夹列表和每个文件夹中的文件列表。

非常感谢任何建议和帮助。

解决方法

如果要使用azcopy上传目录,我们可以使用如下脚本。详情请参阅here

azcopy copy "E:\sampleData" "https://<accountName>.blob.core.windows.net/<path>?<sasToken>" --recursive

enter image description here

此外,您还可以使用以下 PowerShell 脚本来执行此操作

$StorageAccountKey=" "
$sourceFileRootDirectory=" "
$StorageAccountName=" "
$ContainerName=" "
$ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$container = Get-AzureStorageContainer -Name $ContainerName -Context $ctx

 if ($container) {
        $filesToUpload = Get-ChildItem $sourceFileRootDirectory -Recurse -File

        foreach ($x in $filesToUpload) {
            $blobName = ($x.fullname.Substring($x.PSDrive.Root.Length)).Replace("\","/")

            Set-AzureStorageBlobContent -File $x.fullname -Container $container.Name -Blob $blobName -Context $ctx -Force:$Force 
        }
}