Search code examples
azureazure-blob-storageazure-storage

C#: How can I copy an azure storage blob from one account to another cross-tenant?


I know azcopy works cross tenant but I'd need to copy a blob from one storage account to another in a different tenant via C# in my case. Will this library work? https://learn.microsoft.com/en-us/azure/storage/common/storage-use-data-movement-library#copy-a-blob

Or will the storage sdk's StartCopyFromUriAsync work fine? https://learn.microsoft.com/en-us/rest/api/storageservices/copy-blob?tabs=azure-ad

I can't find any references on cross-tenant blob copy compatibility for either sdk online, only for the powershell azcopy cmdlet


Solution

  • With below C# code, I can able to copy an azure storage blob from one account to another cross-tenant.

    {
                CloudStorageAccount sourceAccount =CloudStorageAccount.Parse("Source_conne_tring");
                CloudStorageAccount destinationAccount =CloudStorageAccount.Parse("destination_conne_string");
                CloudBlobClient sourceClient = sourceAccount.CreateCloudBlobClient();
                CloudBlob sourceBlob = sourceClient.GetBlobReferenceFromServer(new Uri("https://source_account.blob.core.windows.net/container/blob")) as CloudBlob;
                CloudBlobClient destinationClient = destinationAccount.CreateCloudBlobClient();
                CloudBlob destinationBlob = destinationClient.GetBlobReferenceFromServer(new Uri("https://destination_account.blob.core.windows.net/container/blob")) as CloudBlob;
    
                Task copyTask = TransferManager.CopyAsync(sourceBlob, destinationBlob, true, null, null);
                await copyTask;
                Console.WriteLine("Successfully copied the blob.");
            }
        }
    

    Reference : Document

    Output: enter image description here

    Portal Output: Successfully uploaded blob enter image description here