Azure Event Hub || Read events from Azure Event Hub using Storage SAS Token

Published 12-19-2019 05:55 AM 1,274 Views

Use Case:

To read the events from Azure Event hub using Storage SAS Token.

 

Pre-Requisites:

  • Azure Event hub Namespace
  • Azure Storage Account SAS Token
  • Console Application to read the published

Steps to follow:

As a part of the working, Azure Event hub uses storage account while reading events to implement features like checkpointing under the Event Processor Host implementation. For this purpose, it is necessary to pass the storage connection string ,this article can be used in scenario where  in we do not want to expose the full storage connection  string and only want to authenticate using storage SAS token.

 

Ready made sample to read events out of event hub using connection string can be found here

 

To generate SAS token for the storage account, we can either use Azure Portal using the Shared Access Signature blade on storage account or use the below piece of code:

 

static string GetAccountSASToken()

{

    const string ConnectionString = "DefaultEndpointsProtocol=https;AccountName=<storage-account>;AccountKey=<account-key>";

    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);

    SharedAccessAccountPolicy policy = new SharedAccessAccountPolicy()

        {

            Permissions = SharedAccessAccountPermissions.Read | SharedAccessAccountPermissions.Write | SharedAccessAccountPermissions.List,

            Services = SharedAccessAccountServices.Blob | SharedAccessAccountServices.File,

            ResourceTypes = SharedAccessAccountResourceTypes.Service,

            SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24),

            Protocols = SharedAccessProtocol.HttpsOnly

        };

    return storageAccount.GetSharedAccessSignature(policy);

}

To pass storage SAS token , we need to initialize the EventProcessorHost instance from the readymade code  in the below fashion:

 

var eventProcessorHost = new EventProcessorHost(new Uri(Uristring), EventHubName,

                PartitionReceiver.DefaultConsumerGroupName,

                TokenProvider.CreateSharedAccessSignatureTokenProvider( KeyName , KeyValue),

                new CloudStorageAccount(new StorageCredentials(StorageSasToken), StorageAccountName, null, true),

                StorageContainerName);

 

The format for the parameters is as below:

Uristring = "sb://{event hub namespace}.servicebus.windows.net"

KeyName = Event hub Policy name

KeyValue = Event hub key value

StorageSasToken =  Storage SAS token provided by Azure Portal or code.

StorageAccountName = Name of Storage account which would be used.

StorageContainerName = Name of Storage container to acquire lease on. 

 

Running the  receive console application, you should be able to read the events from event hub while using SAS token.

Hope this helps!

%3CLINGO-SUB%20id%3D%22lingo-sub-1073988%22%20slang%3D%22en-US%22%3EAzure%20Event%20Hub%20%7C%7C%20Read%20events%20from%20Azure%20Event%20Hub%20using%20Storage%20SAS%20Token%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1073988%22%20slang%3D%22en-US%22%3E%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSTRONG%3E%3CU%3EUse%20Case%3A%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3ETo%20read%20the%20events%20from%20Azure%20Event%20hub%20using%20Storage%20SAS%20Token.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSTRONG%3E%3CU%3EPre-Requisites%3A%3C%2FU%3E%3C%2FSTRONG%3E%3CU%3E%3C%2FU%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CFONT%20size%3D%223%22%3EAzure%20Event%20hub%20Namespace%3C%2FFONT%3E%3C%2FLI%3E%0A%3CLI%3E%3CFONT%20size%3D%223%22%3EAzure%20Storage%20Account%20SAS%20Token%3C%2FFONT%3E%3C%2FLI%3E%0A%3CLI%3E%3CFONT%20size%3D%223%22%3EConsole%20Application%20to%20read%20the%20published%3C%2FFONT%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSTRONG%3E%3CU%3ESteps%20to%20follow%3A%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3EAs%20a%20part%20of%20the%20working%2C%20Azure%20Event%20hub%20uses%20storage%20account%20while%20reading%20events%20to%20implement%20features%20like%20checkpointing%20under%20the%20Event%20Processor%20Host%20implementation.%20For%20this%20purpose%2C%20it%20is%20necessary%20to%20pass%20the%20storage%20connection%20string%20%2Cthis%20article%20can%20be%20used%20in%20scenario%20where%26nbsp%3B%20in%20we%20do%20not%20want%20to%20expose%20the%20full%20storage%20connection%26nbsp%3B%20string%20and%20only%20want%20to%20authenticate%20using%20storage%20SAS%20token.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3EReady%20made%20sample%20to%20read%20events%20out%20of%20event%20hub%20using%20connection%20string%20can%20be%20found%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fevent-hubs%2Fevent-hubs-dotnet-framework-getstarted-send%23receive-events%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehere%3C%2FA%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSTRONG%3ETo%20generate%20SAS%20token%20for%20the%20storage%20account%2C%20we%20can%20either%20use%20Azure%20Portal%20using%20the%20Shared%20Access%20Signature%20blade%20on%20storage%20account%20or%20use%20the%20below%20piece%20of%20code%3A%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3Estatic%20string%20GetAccountSASToken()%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%7B%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20const%20string%20ConnectionString%20%3D%20%22DefaultEndpointsProtocol%3Dhttps%3BAccountName%3D%3CSTORAGE-ACCOUNT%3E%3BAccountKey%3D%3CACCOUNT-KEY%3E%22%3B%3C%2FACCOUNT-KEY%3E%3C%2FSTORAGE-ACCOUNT%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20CloudStorageAccount%20storageAccount%20%3D%20CloudStorageAccount.Parse(ConnectionString)%3B%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20SharedAccessAccountPolicy%20policy%20%3D%20new%20SharedAccessAccountPolicy()%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Permissions%20%3D%20SharedAccessAccountPermissions.Read%20%7C%20SharedAccessAccountPermissions.Write%20%7C%20SharedAccessAccountPermissions.List%2C%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Services%20%3D%20SharedAccessAccountServices.Blob%20%7C%20SharedAccessAccountServices.File%2C%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ResourceTypes%20%3D%20SharedAccessAccountResourceTypes.Service%2C%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20SharedAccessExpiryTime%20%3D%20DateTime.UtcNow.AddHours(24)%2C%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Protocols%20%3D%20SharedAccessProtocol.HttpsOnly%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3B%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20return%20storageAccount.GetSharedAccessSignature(policy)%3B%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%7D%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSTRONG%3ETo%20pass%20storage%20SAS%20token%20%2C%20we%20need%20to%20initialize%20the%20EventProcessorHost%20instance%20from%20the%20readymade%20code%20%26nbsp%3Bin%20the%20below%20fashion%3C%2FSTRONG%3E%3A%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3Evar%20eventProcessorHost%20%3D%20new%20EventProcessorHost(new%20Uri(%3CSTRONG%3EUristring%3C%2FSTRONG%3E)%2C%20EventHubName%2C%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20PartitionReceiver.DefaultConsumerGroupName%2C%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20TokenProvider.CreateSharedAccessSignatureTokenProvider(%20%3CSTRONG%3EKeyName%3C%2FSTRONG%3E%20%2C%20%3CSTRONG%3EKeyValue%3C%2FSTRONG%3E)%2C%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20new%20CloudStorageAccount(new%20StorageCredentials(%3CSTRONG%3EStorageSasToken%3C%2FSTRONG%3E)%2C%20%3CSTRONG%3EStorageAccountName%3C%2FSTRONG%3E%2C%20null%2C%20true)%2C%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3CSTRONG%3EStorageContainerName%3C%2FSTRONG%3E)%3B%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3EThe%20format%20for%20the%20parameters%20is%20as%20below%3A%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSTRONG%3EUristring%3C%2FSTRONG%3E%20%3D%20%22sb%3A%2F%2F%7Bevent%20hub%20namespace%7D.servicebus.windows.net%22%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSTRONG%3EKeyName%3C%2FSTRONG%3E%20%3D%20Event%20hub%20Policy%20name%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSTRONG%3EKeyValue%3C%2FSTRONG%3E%20%3D%20Event%20hub%20key%20value%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSTRONG%3EStorageSasToken%3C%2FSTRONG%3E%20%3D%20%26nbsp%3BStorage%20SAS%20token%20provided%20by%20Azure%20Portal%20or%20code.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSTRONG%3EStorageAccountName%3C%2FSTRONG%3E%20%3D%20Name%20of%20Storage%20account%20which%20would%20be%20used.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%3CSTRONG%3EStorageContainerName%3C%2FSTRONG%3E%20%3D%20Name%20of%20Storage%20container%20to%20acquire%20lease%20on.%26nbsp%3B%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3ERunning%20the%26nbsp%3B%20receive%20console%20application%2C%20you%20should%20be%20able%20to%20read%20the%20events%20from%20event%20hub%20while%20using%20SAS%20token.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%3E%20Hope%20this%20helps!%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1073988%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20Event%20Hub%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eazure%20service%20bus%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20Storage%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Dec 19 2019 05:55 AM
Updated by: