Dave
Dave

Reputation: 313

Copy Azure block blob to Azure file share?

How do I copy a block (or page) blob in azure storage to an Azure file share? My example code works fine if I download the block blob to a local file, but there does not appear to be a method to download to an Azure file share. I've looked at the Azure data movements library but there's no example of how to do this.

void Main()
{
    string myfile =@"Image267.png";

    CredentialEntity backupCredentials = Utils.GetBackupsCredentials();
    CloudStorageAccount backupAccount = new CloudStorageAccount(new StorageCredentials(backupCredentials.Name, backupCredentials.Key), true);
    CloudBlobClient backupClient = backupAccount.CreateCloudBlobClient();
    CloudBlobContainer backupContainer = backupClient.GetContainerReference(@"archive");
    CloudBlockBlob blob = backupContainer.GetBlockBlobReference(myfile);

    CredentialEntity fileCredentails = Utils.GetFileCredentials();
    CloudStorageAccount fileAccount = new CloudStorageAccount(new StorageCredentials(fileCredentails.Name,fileCredentails.Key), true);
    CloudFileClient fileClient =    fileAccount.CreateCloudFileClient();
    CloudFileShare share = fileClient.GetShareReference(@"xfer");

    if (share.Exists())
    {
        CloudFileDirectory rootDir = share.GetRootDirectoryReference();
        CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("hello");
        if (sampleDir.Exists())
        {
             CloudFile file = sampleDir.GetFileReference(myfile);
//           blob.DownloadToFile(file.ToString());
        }       
    }
}

The part that does not work is the commented out line blob.DownloadToFile

Any ideas on how I can do this?

Upvotes: 1

Views: 4880

Answers (2)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29940

There is an example in the official docs here: it's used to copy files from file share to blob storage, but you can make a little change to copy from blob storage to file share. I also write a sample code which is used copy from blob storage to file share, you can take a look at it as below.

You can use SAS token(for the source blob or source file) to copy files to blob / or copy blob to files, in the same storage account or different storage account.

A sample code as below(copy blob to file in the same storage account, and you can make a little change if they are in different storage account):

static void Main(string[] args)
{
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connection string");

    var blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference("t0201");
    CloudBlockBlob sourceCloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("test.txt");

    //Note that if the file share is in a different storage account, you should use CloudStorageAccount storageAccount2 = CloudStorageAccount.Parse("the other storage connection string"), then use storageAccount2 for the file share.
    CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
    CloudFileShare share = fileClient.GetShareReference("testfolder");
    CloudFile destFile = share.GetRootDirectoryReference().GetFileReference("test.txt");
    
    //Create a SAS for the source blob
    string blobSas = sourceCloudBlockBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
    {
        Permissions = SharedAccessBlobPermissions.Read,
        SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24)
    });

    Uri blobSasUri = new Uri(sourceCloudBlockBlob.StorageUri.PrimaryUri.ToString()+blobSas);
    destFile.StartCopy(blobSasUri);

    Console.WriteLine("done now");
    Console.ReadLine();
}

it works well at my side, hope it helps.

Upvotes: 4

There are a few samples in here but in dot net. A non code related alternative would be to use Azcopy it would help transfer data from blob to File Shares and vice versa.

External Suggestions: The following tool called Blobxfer seems to support the transfer but in python.

Upvotes: 1

Related Questions