1

I've got some files sitting in an Azure File storage.

I'm trying to programatically archive them to Azure Blobs and I'm not sure how to do this efficiently.

I keep seeing code samples about copying from one blob container to another blob container .. but not from a File to a Blob.

Is it possible to do without downloading the entire File content locally and then uploading this content? Maybe use Uri's or something?

More Info:

  1. The File and Blob containers are in the same Storage account.
  2. The storage account is RA-GRS

Here was some sample code I was thinking of doing .. but it just doesn't feel right :( (pseudo code also .. with validation and checks omitted).

var file = await ShareRootDirectory.GetFileReference(fileName);
using (var stream = new MemoryStream())
{
    await file.DownloadToStreamAsync(stream);

    // Custom method that basically does:
    // 1. GetBlockBlobReference
    // 2. UploadFromStreamAsync
    await cloudBlob.AddItemAsync("some-container", stream);
}
2
  • If it is useful, please help to mark it as answer that will help more communities who have the same issue.
    – Tom Sun
    Mar 15, 2017 at 8:20
  • 3
    and @TomSun-MSFT .. same goes for the question :) If useful, upvote :)
    – Pure.Krome
    Mar 16, 2017 at 1:08

2 Answers 2

4

How to copy an Azure File to an Azure Blob?

We also can use CloudBlockBlob.StartCopy(CloudFile). CloudFile type is also can be accepted by the CloudBlockBlob.StartCopy function. How to copy CloudFile to blob please refer to document. The following demo code is snippet from the document.

// Parse the connection string for the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    Microsoft.Azure.CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create a CloudFileClient object for credentialed access to File storage.
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

// Create a new file share, if it does not already exist.
CloudFileShare share = fileClient.GetShareReference("sample-share");
share.CreateIfNotExists();

// Create a new file in the root directory.
CloudFile sourceFile = share.GetRootDirectoryReference().GetFileReference("sample-file.txt");
sourceFile.UploadText("A sample file in the root directory.");

// Get a reference to the blob to which the file will be copied.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("sample-container");
container.CreateIfNotExists();
CloudBlockBlob destBlob = container.GetBlockBlobReference("sample-blob.txt");

// Create a SAS for the file that's valid for 24 hours.
// Note that when you are copying a file to a blob, or a blob to a file, you must use a SAS
// to authenticate access to the source object, even if you are copying within the same
// storage account.
string fileSas = sourceFile.GetSharedAccessSignature(new SharedAccessFilePolicy()
{
    // Only read permissions are required for the source file.
    Permissions = SharedAccessFilePermissions.Read,
    SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24)
});

// Construct the URI to the source file, including the SAS token.
Uri fileSasUri = new Uri(sourceFile.StorageUri.PrimaryUri.ToString() + fileSas);

// Copy the file to the blob.
destBlob.StartCopy(fileSasUri);

Note:

If you are copying a blob to a file, or a file to a blob, you must use a shared access signature (SAS) to authenticate the source object, even if you are copying within the same storage account.

2
  • 1
    The code above will not work as is. One would need to create a SAS URI for the file and use that in StartCopy(). Please update your answer accordingly. Mar 13, 2017 at 12:22
  • You are right. I have updated the answer. Thank you.
    – Tom Sun
    Mar 13, 2017 at 13:15
1

Use the Transfer Manager: https://msdn.microsoft.com/en-us/library/azure/microsoft.windowsazure.storage.datamovement.transfermanager_methods.aspx

There are methods to copy from CloudFile to CloudBlob.

Add the "Microsoft.Azure.Storage.DataMovement" nuget package

using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.File;
using Microsoft.WindowsAzure.Storage.DataMovement;

private string _storageConnectionString = "your_connection_string_here";

public async Task CopyFileToBlob(string blobContainer, string blobPath, string fileShare, string fileName)
{
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_connectionString);
    CloudFileShare cloudFileShare = storageAccount.CreateCloudFileClient().GetShareReference(fileShare);
    CloudFile source = cloudFileShare.GetRootDirectoryReference().GetFileReference(fileName);

    CloudBlobContainer blobContainer = storageAccount.CreateCloudBlobClient().GetContainerReference(blobContainer);
    CloudBlockBlob target = blobContainer.GetBlockBlobReference(blobPath);

    await TransferManager.CopyAsync(source, target, true);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.