Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 将文件和文件夹上载到Azure Blob存储_Powershell_Azure Storage Account - Fatal编程技术网

Powershell 将文件和文件夹上载到Azure Blob存储

Powershell 将文件和文件夹上载到Azure Blob存储,powershell,azure-storage-account,Powershell,Azure Storage Account,我已经在Azure中创建了一个存储帐户并创建了一个容器。我正在尝试上载存储在服务器中的文件。这些文件存储在800个文件夹中。 我已尝试使用此Powershell脚本执行此操作,但它不适用于子文件夹 $LocalFolder = "\\Server\Data" Add-AzureAccount # Set a default Azure subscription. Select-AzureSubscription -SubscriptionName 'nameofsubscription

我已经在Azure中创建了一个存储帐户并创建了一个容器。我正在尝试上载存储在服务器中的文件。这些文件存储在800个文件夹中。 我已尝试使用此Powershell脚本执行此操作,但它不适用于子文件夹

$LocalFolder = "\\Server\Data" 

Add-AzureAccount 


# Set a default Azure subscription.
Select-AzureSubscription -SubscriptionName 'nameofsubscription' –Default


$Key = Get-AzureStorageKey -StorageAccountName  mydatastorename

$Context = New-AzureStorageContext -StorageAccountKey $Key.Primary -StorageAccountName mydatastorename

foreach ($folder in Get-ChildItem $LocalFolder)
    {

            ls –Recurse –Path $LocalFolder |Set-AzureStorageBlobContent  -Container nameofcontainer -Context $Context -BlobType Block
    }
如果将$LocalFolder设置为“\Server\Data\subfolders001”,子文件夹001中的文件将上载到容器中。但当我将其保留为“\Server\Data”时,它就不起作用了

我希望脚本将其中的所有子文件夹和文件上载到存储容器中

我已经添加了运行时得到的输出

我没有收到任何错误消息,但每个子文件夹都有一条警告消息

 WARNING: Can not upload the directory '\\Server\Data\subfolders001' to azure. If you want to upload directory, please use "ls -File -Recurse | Set-AzureStorageBlobContent -Container containerName".

等待一段时间后,我不得不停止powershell脚本。Azure Blob存储只有一级文件夹-容器

相反,您可以使用以下几个选项

  • 使用Azure文件服务
  • 使用此处描述的变通方法

    • 我自己刚刚解决了这个问题,请参见下面我的解决方案:

      $StorageAccountName = "storageAccountName"
      $StorageAccountKey = "StorageAccountKey"
      $ContainerName = "ContainerName"
      $sourceFileRootDirectory = "AbsolutePathToStartingDirectory" # i.e. D:\Docs
      
      function Upload-FileToAzureStorageContainer {
          [cmdletbinding()]
          param(
              $StorageAccountName,
              $StorageAccountKey,
              $ContainerName,
              $sourceFileRootDirectory,
              $Force
          )
      
          $ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
          $container = Get-AzureStorageContainer -Name $ContainerName -Context $ctx
      
          $container.CloudBlobContainer.Uri.AbsoluteUri
          if ($container) {
              $filesToUpload = Get-ChildItem $sourceFileRootDirectory -Recurse -File
      
              foreach ($x in $filesToUpload) {
                  $targetPath = ($x.fullname.Substring($sourceFileRootDirectory.Length + 1)).Replace("\", "/")
      
                  Write-Verbose "Uploading $("\" + $x.fullname.Substring($sourceFileRootDirectory.Length + 1)) to $($container.CloudBlobContainer.Uri.AbsoluteUri + "/" + $targetPath)"
                  Set-AzureStorageBlobContent -File $x.fullname -Container $container.Name -Blob $targetPath -Context $ctx -Force:$Force | Out-Null
              }
          }
      }
      
      运行:

      Upload-FileToAzureStorageContainer -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey -ContainerName $ContainerName -sourceFileRootDirectory $sourceFileRootDirectory -Verbose
      
      您应该看到,它在运行时打印以下输出:

      VERBOSE: Uploading testfile.json to https://storagecontainername.blob.core.windows.net/path/testfile.json
      VERBOSE: Performing the operation "Set" on target "/path/testfile.json".
      ICloudBlob        : Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob
      BlobType          : BlockBlob
      Length            : 0
      ContentType       : application/octet-stream
      LastModified      : 23/03/2017 14:20:53 +00:00
      SnapshotTime      :
      ContinuationToken :
      Context           : Microsoft.WindowsAzure.Commands.Common.Storage.AzureStorageContext
      Name              : /path/testfile.json
      
      VERBOSE: Transfer Summary
      --------------------------------
      Total:  1.
      Successful: 1.
      Failed: 0.
      
      我承认这并不完美,但只要稍加努力,您就可以扩展它以适应更复杂的场景文件上载,但如果您想将目录的内容上载到目标存储容器,它应该可以做到这一点

      Azure存储容器本质上是不能包含其他存储容器的目录。因此,为了在Azure存储容器中实现文件夹结构,您需要在文件前面加上开始搜索的根文件夹的路径。即,如果您的根是:

      D:\Files
      
      文件包括:

      Folder 1
        -> File 1.txt
      File 2.txt
      
      您需要将目标路径设置为

      $StorageContainerObject.CloudBlobContainer.Uri.AbsoluteUri + "\Files\Folder 1\File 1.txt"
      $StorageContainerObject.CloudBlobContainer.Uri.AbsoluteUri + "\Files\File 2.txt"
      
      如果使用Azure storage Explorer之类的工具查看存储容器,则即使文件都存储在一个容器中,它也会识别文件结构

      谢谢


      Jamie

      请发布运行当前脚本时收到的错误。