0

I am having issues when it comes to deploying to azure, It is unable to configure for the storage account as I used an environment variable to store it which works perfectly for local use.

Where is the best place to configure a blob storage connection string - I have already got a connection string for an sql database stored in appsettings.json can I store the connection string for the blob storage there as well ?

I tried to use this link to get an understanding but couldn't quite grasp it

2
  • 1
    is it a .net core web project? Commented Feb 19, 2021 at 1:23
  • @IvanYang Yes it is. Commented Feb 22, 2021 at 20:15

2 Answers 2

1

If it's a .net core azure web app, you can still save the storage account connection string as environment variable.

For example, after deploying it to azure, then in azure portal -> your azure web app -> in the left pane, click Configuration -> Then in the Application settings, click New application setting -> then put your storage account connection string there -> click OK button, at last click Save button. The screenshot is as below:

enter image description here

Then in your code, you can still use the code below to read the storage account connection string:

Environment.GetEnvironmentVariable("myStorageAccount_connstr",EnvironmentVariableTarget.Process);
Sign up to request clarification or add additional context in comments.

Comments

0

Copying storage account key locally is not the best idea because of security risks, better generate a SAS token:

Go to Portal -> storage account -> Shared access signature -> Generate SAS token.

Generate storage account SAS

Then use generated SAS token to connect to blob storage. Below there is a C# program that creates a new storage blob from the local file. To run the program first install the packages Microsoft.Azure.Storage.Blob and Microsoft.Azure.Storage.Common and replace you SAS token

using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Auth;
using Microsoft.Azure.Storage.Blob;

class Program
{
    static void Main(string[] args)
    {
        const string sasToken = "?sv=2020-02-10&ss=bfqt&srt=sco&sp=rwdlacupx&se=2021-02-25T06:53:06Z&st=2021-02-17T22:53:06Z&spr=https&sig=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        StorageCredentials storageCredentials = new StorageCredentials(sasToken);

        const string accountName = "my-storage-account";
        const string blobContainerName = "my-container";
        const string blobName = "new-new-blob";
        const string myFileLocation = @"f:\my-local-file.txt";

        var storageAccount = new CloudStorageAccount(storageCredentials, accountName, null, true);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer blobContainer = blobClient.GetContainerReference(blobContainerName);
        blobContainer.CreateIfNotExists();
        CloudBlockBlob cloudBlob = blobContainer.GetBlockBlobReference(blobName);
        cloudBlob.UploadFromFile(myFileLocation);
    }
}

Alternatively, you can Acquire a token from Azure AD for authorizing requests from a client application. Also you can read more about https://learn.microsoft.com/en-us/azure/storage/common/storage-sas-overview

2 Comments

Thank you for the response, Is it not bad practice to use the token in the code directly ?
Okay thank you ! So a token is nothing like a connection string or a key ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.