Home > Blockchain >  .NET - upload file to Azure blob storage using SAS token
.NET - upload file to Azure blob storage using SAS token

Time:02-01

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).

CodePudding user response:

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=/azure/storage/blobs/toc.json&bc=/azure/storage/blobs/breadcrumb/toc.json&tabs=dotnet

CodePudding user response:

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=";
            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:\Users\Downloads\textfile");
                myStream.Write(bytes, 0, 1474);
            }
            Console.ReadLine();

        }
    }
}

Output:

enter image description here

  • Related