2
votes

guys,

I've got code making an API call, getting JSON back, parsing it and storing it in an SQL table. Now I'd like to take that JSON and also save it in Azure blob storage, then save a reference to it in the SQL table as a separate field.

So far, I've got this:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("MyConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("mydata");
CloudBlockBlob blockBlob = container.GetBlockBlobReference(dataString);
blockBlob.UploadTextAsync(resultContent);

What can I save as the reference string, for future retrieval of the blob? Is it blockBlob?

2

2 Answers

2
votes

Every blob will have a uri like:

https://<yourstorageaccountname>.blob.core.windows.net/containername/blobname

This is what you would store in your database. You can even use this uri to provide a direct link, within your app, to an end-user directly (e.g. as a link in a web page). Note: for the direct-link to work externally (e.g. without storage account credentials), the container would need to be public, or you'd need to create a Shared Access Signature to allow for temporary public access, if it's in a private container.

Arguably, you can store just the container/blob name, but then, if you have multiple storage accounts, you'll need to know which one you stored it in.

1
votes

Your code can be changed like following:

CloudBlockBlob blockBlob = container.GetBlockBlobReference("datastring.json");

You can later retrieve this with the same name i.e. datastring.json, and you can save the same blob name in your database.