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
You can use the Sas Token as authentication for BlobServiceClient:
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
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:
Output: