Search code examples
c#azureazure-storageazure-blob-storage

Copy file from one Azure storage account to another


I am trying to copy a file from one storage account to another account using StartCopy method to copy the file. Check the below code.

CloudStorageAccount sourceStorageAccount = CloudStorageAccount.Parse(@"source storage account connection string");
CloudStorageAccount destStorageAccount = CloudStorageAccount.Parse(@"destination storage account connection string");

CloudBlobClient sourceBlobClient = sourceStorageAccount.CreateCloudBlobClient();
CloudBlobClient destBlobClient = destStorageAccount.CreateCloudBlobClient();
var sourceContainer = sourceBlobClient.GetContainerReference("sourceContainer");
var destContainer = destBlobClient.GetContainerReference("destContainer");

CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference("copy.txt");
CloudBlockBlob targetBlob = destContainer.GetBlockBlobReference("copy.txt");

targetBlob.StartCopy(sourceBlob);

But it always return the following error.

Microsoft.WindowsAzure.Storage.StorageException: 'The remote server returned an error: (404) Not Found.'

What am I missing here ?

Note, the same code works perfectly if I try to copy files from one container to another within same storage account.


Solution

  • Take a look at the following example on how a copy should be performed (taken from Introducing Asynchronous Cross-Account Copy Blob):

    public static void CopyBlobs(
                CloudBlobContainer srcContainer,  
                string policyId, 
                CloudBlobContainer destContainer)
    {
        // get the SAS token to use for all blobs
        string blobToken = srcContainer.GetSharedAccessSignature(
                       new SharedAccessBlobPolicy(), policyId);
    
    
        var srcBlobList = srcContainer.ListBlobs(true, BlobListingDetails.None);
        foreach (var src in srcBlobList)
        {
            var srcBlob = src as CloudBlob;
    
            // Create appropriate destination blob type to match the source blob
            CloudBlob destBlob;
            if (srcBlob.Properties.BlobType == BlobType.BlockBlob)
            {
                destBlob = destContainer.GetBlockBlobReference(srcBlob.Name);
            }
            else
            {
                destBlob = destContainer.GetPageBlobReference(srcBlob.Name);
            }
    
            // copy using src blob as SAS
            destBlob.StartCopyFromBlob(new Uri(srcBlob.Uri.AbsoluteUri + blobToken));
        }
    }
    

    Hope it helps!