Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programatically get my Azure storage endpoint URLs?

I'm fairly new to Azure and I'm using Blob storage to store some binary images, but I can't seem to figure out how to programatically get my Azure storage endpoint URLs?

I am trying to render out some <img> tags that render correctly locally using the simulator, or when deployed to my live Azure instance.

So for example if I launch my site locally it would render out as:

<img src="http://127.0.0.1:10000/devstoreaccount1/uploads/image.jpg" />

and when deployed to Azure it would render out as:

<img src="http://example.blob.core.windows.net/uploads/image.jpg" />

Obviously one way would be to just store it as a setting in my ServiceConfiguration.Local.cscfg and ServiceConfiguration.Cloud.cscfg files, but there must be a way to get this value programatically.

enter image description here

like image 873
David P Avatar asked Mar 23 '26 20:03

David P


1 Answers

You're correct in that your connection strings should be stored in your configuration, and you can have different values for local and cloud. This is very simple to get at, programmatically:

First, make sure you set a config setting publisher during role startup:

CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)));

Now just retrieve your storage account and pull the URL:

var storageAccount = CloudStorageAccount.FromConfigurationSetting("MyStorage");
var urlBase = storageAccount.BlobEndpoint;

At this point, you'd base your IMG tags off urlBase.

like image 178
David Makogon Avatar answered Mar 25 '26 14:03

David Makogon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!