user2463514
user2463514

Reputation: 273

Copy large file between azure storage account gen 2 using c# code

We have files store in azure storage account gen 2

We are using api approach to create,delete and read the files [ as mention here Read File ]

We are trying to copying the file from one storage account to another using api approach. Can someone suggest fast approach to achieve it ?

Note:

Upvotes: 0

Views: 2607

Answers (2)

Vlad DX
Vlad DX

Reputation: 4730

Actually, it's very hard to find the working solution because the official documentation is outdated and there is a lack of any up-to-date examples there.

Outdated way

An outdated example of working with blob containers could be found here: https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-dotnet?tabs=windows

That example uses WindowsAzure.Storage NuGet package that was renamed to Microsoft.Azure.Storage.* and split to separate packages.

Up-to-date solution

I'm currently working on the deployment of the static SPA to Azure Blob storage. It has a very nice feature "Static website" that serves the files.

There is a working example that could be used to copy all contents from one blob container to another. Please consider it as a hint (not production ready).

All you need is to:

  • Have an existing blob container.
  • Install Microsoft.Azure.Storage.DataMovement NuGet package.
  • Provide a proper connection string.

Here is the code:

// I left fully qualified names of the types to make example clear.

var connectionString = "Connection string from `Azure Portal > Storage account > Access Keys`";

var sourceContainerName = "<source>";
var destinationContainerName = "<destination>";

var storageAccount = Microsoft.Azure.Storage.CloudStorageAccount.Parse(connectionString);

var client = storageAccount.CreateCloudBlobClient();

var sourceContainer = client.GetContainerReference(sourceContainerName);

var destinationContainer = client.GetContainerReference(destinationContainerName);
// Create destination container if needed
await destinationContainer.CreateIfNotExistsAsync();

var sourceBlobDir = sourceContainer.GetDirectoryReference(""); // Root directory
var destBlobDir = destinationContainer.GetDirectoryReference("");

// Use UploadOptions to set ContentType of destination CloudBlob
var options = new Microsoft.Azure.Storage.DataMovement.CopyDirectoryOptions
{
    Recursive = true,
};

var context = new Microsoft.Azure.Storage.DataMovement.DirectoryTransferContext();

// Perform the copy
var transferStatus = await Microsoft.Azure.Storage.DataMovement.TransferManager
    .CopyDirectoryAsync(sourceBlobDir, destBlobDir, true, options, context);

Upvotes: 1

George Chen
George Chen

Reputation: 14334

you could use AzCopy to transfer data. You could copy data between a file system and a storage account, or between storage accounts with AzCopy.

About the details how to use AzCopy you could refer to this official doc. In this doc , there are download link and the tutorials.

Update: About transfer files between file shares you could refer to this code:

AzCopy /Source:https://myaccount1.file.core.windows.net/myfileshare1/ /Dest:https://myaccount2.file.core.windows.net/myfileshare2/ /SourceKey:key1 /DestKey:key2 /S

Other about Copy files in File Storage you could refer to the doc.

If you still have other questions, please let me know. Hope this could help you.

Upvotes: 1

Related Questions