简体   繁体   中英

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

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

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-do.net?toc=%2Fazure%2Fstorage%2Fblobs%2Ftoc.json&bc=%2Fazure%2Fstorage%2Fblobs%2Fbreadcrumb%2Ftoc.json&tabs=do.net

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

        }
    }
}

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM