【问题标题】:Upload files and folder into Azure Blob Storage将文件和文件夹上传到 Azure Blob 存储
【发布时间】:2017-03-24 17:36:36
【问题描述】:

我在 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”,则 subfolder001 中的文件将上传到容器。但是当我将它保留为“\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 脚本。

【问题讨论】:

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

标签: powershell azure-storage-account


【解决方案1】:

只是自己解决了这个问题,请参阅下面的解决方案:

$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 存储容器中实现文件夹结构,您需要在文件前面加上您开始搜索的根文件夹的路径。 IE。如果你的根是:

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 存储资源管理器等工具查看存储容器,那么即使文件都存储在一个容器中,它也会识别文件结构。

谢谢,

杰米

【讨论】:

    【解决方案2】:

    Azure Blob 存储只有一层文件夹 - 容器

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

    【讨论】:

      猜你喜欢
      • 2021-04-30
      • 2017-01-24
      • 2017-08-19
      • 2015-05-08
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      相关资源
      最近更新 更多