简体   繁体   中英

Move a File from Azure File Storage to Azure Blob Storage

I have rather foolishly uploaded a vhd to Azure file storage thinking I can create a virtual machine from it only to find out it really needs to be in blob storage.

I know I can just upload it again - but it is very large and my upload speed is very slow.

My question is - can I move a file from file storage to blob storage without downloading/uploading again? Ie is there anything in the Azure portal UI to do it, or even a PowerShell command?

You can try AzCopy :

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

When copying from File Storage to Blob Storage, the default blob type is block blob, user can specify option /BlobType:page to change the destination blob type.

AzCopy by default copies data between two storage endpoints asynchronously. Therefore, the copy operation will run in the background using spare bandwidth capacity that has no SLA in terms of how fast a blob will be copied, and AzCopy will periodically check the copy status until the copying is completed or failed. The /SyncCopy option ensures that the copy operation will get consistent speed.

In 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);
    }
}

The code to copy:

// 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);

Another option is to use Azure CLI...

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

More info here: az storage copy

Thanks to Gaurav Mantri for pointing me in the direction of AzCopy.

This does allow me to copy between file and blob storage using the command:

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

However as Gaurav also rightly points out in the comment the resulting blob will be of type Block Blob and this is no good for me. I need one of type Page Blob in order to create a VM out of it using https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-specialized-vhd

There is no way to change the blob type as far as I can see once it is up there in the cloud, so it looks like my only option is to wait for a lengthy upload again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM