将文件从Azure文件存储移动到Azure Blob存储

时间:2017-03-25 18:52:39

标签: azure azure-storage

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

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

我的问题是 - 我可以将文件从文件存储移动到blob存储而无需再次下载/上传吗?即Azure门户UI中是否有任何内容可以执行此操作,甚至是PowerShell命令?

4 个答案:

答案 0 :(得分:3)

您可以尝试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选项可确保复制操作获得一致的速度。

答案 1 :(得分:1)

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

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

此处有更多信息:az storage copy

答案 2 :(得分:0)

感谢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类型,这对我没有好处。我需要一种类型的页面Blob,以便使用https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-specialized-vhd

创建一个VM

一旦它出现在云中,就无法改变blob类型,所以看起来我唯一的选择是等待再次上传很长时间。

答案 3 :(得分:0)

在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);
相关问题