Search code examples
azure-blob-storageazure-storageazure-sas

Azure Stroage blob: Copying file with the SAS Token for Delegate users using .NET Client Library


I am copying file from one Storage container to another container in Azure Storage blob. I was getting errors for Authorization. The solution to overcover the issue was to use SAS token. See the below link to Stackoverflow the question and solution. I was able to get the SAS token and successfully copy the file from one container to another or in the same storage account. But when working with the User Delegate Sas Token I am getting the same error again. I am most likely making a mistake at some point or missing. I am posting the code. I took the help from the post mentioned below.

Getting error when copying file from one container to another container in Azure Storage using .Net client library

       public void copyblob3(string fileName)
    {

        var stageContainer = "stageContainer";//stage
        var targetContainer = "targetContainer";


        var storage = "storageAccountName";
        url = @"https://" + storage + ".blob.core.windows.net";
        blobServiceClient = new BlobServiceClient(
          new Uri(url),
      new DefaultAzureCredential());


        BlobContainerClient stageContainerClient = blobServiceClient.GetBlobContainerClient(stageContainer);         
        BlobContainerClient targetContainerClient = blobServiceClient.GetBlobContainerClient(targetContainer);

        var targetBlobClient = targetContainerClient.GetBlobClient(fileName);  
        DateTimeOffset expiryTime = DateTimeOffset.UtcNow.AddMinutes(5);
            var userDelegationKey = blobServiceClient.GetUserDelegationKey(null, expiryTime, CancellationToken.None);
            var sasBuilder = new BlobSasBuilder()
            {
                BlobContainerName = stageContainer,
                BlobName = fileName,
                Resource = "b",                 
                ExpiresOn= DateTimeOffset.UtcNow.AddDays(1),

            };
            sasBuilder.SetPermissions(BlobSasPermissions.Read);
        BlobClient stageBlobClient = stageContainerClient.GetBlobClient(fileName);       
            var blobUriBuilder = new BlobUriBuilder(stageBlobClient.Uri)
            {
                Sas = sasBuilder.ToSasQueryParameters(userDelegationKey,
                    stageContainer)
            };
             blobUriBuilder.ToUri();  **// I may not be using this Uri correctly//**

        var result = targetBlobClient.StartCopyFromUri(**stageBlobClient.Uri**);


        }

Solution

  • Copying file with the SAS Token for Delegate users using .NET Client Library

    You can use the below code to copy the file from one container to another container with User Delegation Key using the .NET library.

    Code:

    using Azure.Identity;
    using Azure.Storage.Blobs;
    using Azure.Storage.Sas;
    using System;
    
    
    class program {
        public static void Main(string[] args)
        {
            var stageContainer = "test";
            var targetContainer = "test1";
            var storage = "venkat123";
            var fileName = "day.csv";
            var url = @"https://" + storage + ".blob.core.windows.net";
            var blobServiceClient = new BlobServiceClient(
                new Uri(url),
                new DefaultAzureCredential());
    
            BlobContainerClient stageContainerClient = blobServiceClient.GetBlobContainerClient(stageContainer);
            BlobContainerClient targetContainerClient = blobServiceClient.GetBlobContainerClient(targetContainer);
    
            var targetBlobClient = targetContainerClient.GetBlobClient(fileName);
            var userDelegationKey = blobServiceClient.GetUserDelegationKey(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddDays(1));
            var sasBuilder = new BlobSasBuilder()
            {
                BlobContainerName = stageContainer,
                BlobName = fileName,
                Resource = "b",
                StartsOn = DateTimeOffset.UtcNow,
                ExpiresOn = DateTimeOffset.UtcNow.AddDays(1),
            };
            sasBuilder.SetPermissions(BlobSasPermissions.Read | BlobSasPermissions.Write); 
            BlobClient stageBlobClient = stageContainerClient.GetBlobClient(fileName);
            var blobUriBuilder = new BlobUriBuilder(stageBlobClient.Uri)
            {
                Sas = sasBuilder.ToSasQueryParameters(userDelegationKey,blobServiceClient.AccountName)
            };
            var sourceUri = blobUriBuilder.ToUri();
            Console.WriteLine(sourceUri);
    
            var result = targetBlobClient.StartCopyFromUri(sourceUri);
            Console.WriteLine("The file is copied to target container!!!");
        }
    }
     
    

    Output:

    https://venkat123.blob.core.windows.net/test/day.csv?skoid=6e19aa9exxxc&sktid=72f988bf-86f1xxdb47&skt=2023-06-14T04%3A26%3A48Z&ske=2023-06-15T04%3A26%3A48Z&sks=b&skv=2023-01-03&sv=2023-01-03&st=2023-06-14T04%3A27%3A06Z&se=2023-06-15T04%3A27%3A06Z&sr=b&sp=rw&sig=xxxxxxxxx
    The file is copied to target container!!!
    

    enter image description here

    Portal: enter image description here