skip to Main Content

I use C# (.NET 6) for uploading files to an Azure storage account. It works fine with a storage connection string, no problem at all. Now I need to do the same with a SAS token. I tried lots of examples from Google, I always end up with this exception:

An exception of type ‘System.FormatException’ occurred in Azure.Storage.Blobs.dll but was not handled in user code: ‘No valid combination of account information found.’

I generate the SAS token on the container I need to upload to, I set the validity start to past, I choose "Create" permission only (the users can only add new files to the container, nothing else). I use the Blob SAS URL in the connection string. On this code it fails:

BlobClient blobClient = new BlobClient(
            connectionString: connectionString,
            blobContainerName: containerName,
            blobName: dayStamp
        );

Any ideas what I’m doing wrong? Or can you point me to some step by step instructions how to upload a file using SAS token?
The storage account has Hierarchical namespaces enabled (not sure if it is relevant when setting SAS on a container level).


Edit: I prefer to work with the Azure.Storage.Blobs package.

2

Answers


  1. You can use the Sas Token as authentication for BlobServiceClient:

    var blobServiceClient = new BlobServiceClient
            (new Uri($"{blobServiceUri}?{sasToken}"), null);
    
    BlobContainerClient blobContainer = blobServiceClient.GetBlobContainerClient("container");            
    

    Source: https://learn.microsoft.com/en-us/azure/storage/common/storage-account-sas-create-dotnet?toc=%2Fazure%2Fstorage%2Fblobs%2Ftoc.json&bc=%2Fazure%2Fstorage%2Fblobs%2Fbreadcrumb%2Ftoc.json&tabs=dotnet

    Login or Signup to reply.
  2. I do agree with @Thiago Custodio and @Gaurav Mantri, you can also use my approach as an alternative to upload a text file to Storage account using SAS Token and I followed Blog and MQ and A:

    using Azure.Storage.Blobs;
    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Auth;
    using Microsoft.WindowsAzure.Storage.Blob;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp4
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                const string accountName = "rithwik";
                const string blobContainerName = "rithwik";
                const string sasToken = @"sp=ac:11Z&se=2023-02-01T18:32:11Z&spr=https&sv=2021-06-08&sr=cNw%3D";
                var accountSAS = new StorageCredentials(sasToken);
                var storageAccount = new CloudStorageAccount(accountSAS, accountName, null, true);
                var blobClient = storageAccount.CreateCloudBlobClient();
                var container = blobClient.GetContainerReference(blobContainerName);
                //Console.WriteLine(container);
                var blockBlob = container.GetBlockBlobReference("textfile.txt");
                using (Stream myStream = await blockBlob.OpenWriteAsync())
                {
                    var bytes = File.ReadAllBytes(@"C:UsersDownloadstextfile");
                    myStream.Write(bytes, 0, 1474);
                }
                Console.ReadLine();
    
            }
        }
    }
    

    Output:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search