【问题标题】:Move a File from Azure File Storage to Azure Blob Storage将文件从 Azure 文件存储移动到 Azure Blob 存储
【发布时间】:2017-08-18 15:13:02
【问题描述】:

我相当愚蠢地将 vhd 上传到 Azure 文件存储,以为我可以从中创建一个虚拟机,结果却发现它确实需要在 blob 存储中。

我知道我可以再次上传它 - 但它非常大而且我的上传速度很慢。

我的问题是 - 我可以在不重新下载/上传的情况下将文件从文件存储移动到 Blob 存储吗? IE。 Azure 门户 UI 中是否有任何内容可以执行此操作,甚至是 PowerShell 命令?

【问题讨论】:

  • 看看 AzCopy。我不在电脑前,所以我无法分享确切的细节,但应该可以将文件从文件存储复制到 blob 存储。但是请记住,复制的 blob 将是“BlockBlob”类型,而不是“PageBlob”类型。如果您希望它为“PageBlob”,那么您需要重新上传它。

标签: azure azure-storage


【解决方案1】:

你可以试试AzCopy:

AzCopy.exe /Source:{*URL to source container*} /Dest:{*URL to dest container*} /SourceKey:{*key1*} /DestKey:{*key2*} /S

从文件存储复制到 Blob 存储时,默认的 blob 类型为块 blob,用户可以指定选项 /BlobType:page 来更改目标 blob 类型。

默认情况下,AzCopy 在两个存储终结点之间异步复制数据。因此,复制操作将使用没有 SLA 的备用带宽容量在后台运行,就 blob 的复制速度而言,AzCopy 将定期检查复制状态,直到复制完成或失败。 /SyncCopy 选项确保复制操作将获得一致的速度。

【讨论】:

    【解决方案2】:

    在 C# 中:

    public static CloudFile GetFileReference(CloudFileDirectory parent, string path)
    {
        var filename = Path.GetFileName(path);
        var fullPath = Path.GetDirectoryName(path);
        if (fullPath == string.Empty)
        {
            return parent.GetFileReference(filename);
        }
        var dirReference = GetDirectoryReference(parent, fullPath);
        return dirReference.GetFileReference(filename);
    }
    
    public static CloudFileDirectory GetDirectoryReference(CloudFileDirectory parent, string path)
    {
        if (path.Contains(@"\"))
        {
            var paths = path.Split('\\');
            return GetDirectoryReference(parent.GetDirectoryReference(paths.First()), string.Join(@"\", paths.Skip(1)));
        }
        else
        {
            return parent.GetDirectoryReference(path);
        }
    }
    

    要复制的代码:

    // Source File Storage
    string azureStorageAccountName = "shareName";
    string azureStorageAccountKey = "XXXXX";
    string name = "midrive";
    CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(azureStorageAccountName, azureStorageAccountKey), true);
    CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
    CloudFileShare fileShare = fileClient.GetShareReference(name);
    CloudFileDirectory directorio = fileShare.GetRootDirectoryReference();
    CloudFile cloudFile = GetFileReference(directorio, "SourceFolder\\fileName.pdf");
    
    // Destination Blob
    string destAzureStorageAccountName = "xx";
    string destAzureStorageAccountKey = "xxxx";
    CloudStorageAccount destStorageAccount = new CloudStorageAccount(new StorageCredentials(destAzureStorageAccountName, destAzureStorageAccountKey), true);
    CloudBlobClient destClient = destStorageAccount.CreateCloudBlobClient();
    CloudBlobContainer destContainer = destClient.GetContainerReference("containerName");
    CloudBlockBlob destBlob = destContainer.GetBlockBlobReference("fileName.pdf");
    
    // copy
    await TransferManager.CopyAsync(cloudFile, destBlob, true);
    

    【讨论】:

      【解决方案3】:

      另一种选择是使用 Azure CLI...

      az storage copy -s /path/to/file.txt -d https://[account].blob.core.windows.net/[container]/[path/to/blob]
      

      更多信息在这里:az storage copy

      【讨论】:

        【解决方案4】:

        感谢 Gaurav Mantri 为我指明 AzCopy 的方向。

        这确实允许我使用以下命令在文件和 blob 存储之间进行复制:

        AzCopy.exe /Source:*URL to source container* /Dest:*URL to dest container* /SourceKey:*key1* /DestKey:*key2* /S
        

        但是,正如 Gaurav 在评论中正确指出的那样,生成的 blob 将是 Block Blob 类型,这对我没有好处。我需要一种 Page Blob 类型,以便使用 https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-specialized-vhd 从中创建 VM

        据我所知,一旦 blob 位于云中,就无法更改它的类型,因此看来我唯一的选择是再次等待漫长的上传。

        【讨论】:

        • 其实你可以根据aka.ms/azcopy将文件复制到page blob:“从File Storage复制到Blob Storage时,默认的blob类型是block blob,用户可以指定选项/BlobType:page来改变目标 blob 类型。”
        • 啊哈 - 那会做得很完美!我最后重新上传了,一切都很好。如果你想写你的评论作为答案 - 我认为这是正确的......
        • 发表了我的答案:)
        • "命令的语法不正确。不支持从文件存储异步复制到 Blob 存储作为页 blob。" --AzCopy
        猜你喜欢
        • 2017-01-19
        • 2021-11-28
        • 2018-01-25
        • 2020-08-25
        • 2018-09-25
        • 2013-02-06
        • 2020-08-18
        • 2020-02-29
        • 2021-07-29
        相关资源
        最近更新 更多