How to connect to a Azure File Store from a .NET Core 3.1 console app in VS2019

1.3k views Asked by At

I have the task of making a .NET Core 3.1 console app that will run in a linux docker container on the AWS platform, connect to a Azure File Store to read and write files. I have am a C# programmer but have not had anything to do with the world of containers or Azure as yet.

I have received a Azure connection string in the following format: DefaultEndpointsProtocol=https;AccountName=[ACCOUNT_NAME_HERE];AccountKey=[ACCOUNT_KEY_HERE];EndpointSuffix=core.windows.net

But following the examples I have seen online like this:

https://learn.microsoft.com/en-us/visualstudio/azure/vs-azure-tools-connected-services-storage?view=vs-2019

  1. Right click on project in VS2019 and add Connected Service.
  2. Select Azure Storage from the list.
  3. Connect to you Azure Storage account

For step 3 you need to use an Azure Account login email/pass.

I don't have that, I just have a connection string.

I have found examples like the following:

http://www.mattruma.com/adventures-with-azure-storage-accessing-a-file-with-a-shared-access-signature/

https://www.c-sharpcorner.com/article/implement-azure-file-storage-using-asp-net-core-console-application/

But these both use:

Microsoft.Azure.Storage.Common

and under dependencies it has .NET Standard and .NET Framework. I don't think these will run in the linux docker container. Once I have worked out docker containers work I will do a test to confirm this.

Can anyone shed some light on how I can, from a .NET Core 3.1 console app running in a linux docker container on the AWS platform, connect to a Azure File Store to read and write files using the Azure connection string format outlined above?

1

There are 1 answers

3
Cindy Pau On BEST ANSWER

If you focus on how to add service dependencies in Connected Service, then if you don't have the Azure Account login email/pass you can not add the service.

From your description, seems you just want to read and write files from files share of storage using C# console app. So you don't need to add service of project, just add the code in your console app is ok.

This is a simple code:

using System;
using System.IO;
using System.Text;
using Azure;
using Azure.Storage.Files.Shares;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            string con_str = "DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx;EndpointSuffix=core.windows.net";
            string sharename = "test";
            string filename = "test.txt";
            string directoryname = "testdirectory";

            ShareServiceClient shareserviceclient = new ShareServiceClient(con_str);
            ShareClient shareclient = shareserviceclient.GetShareClient(sharename);
            ShareDirectoryClient sharedirectoryclient = shareclient.GetDirectoryClient(directoryname);

            //write data.
            ShareFileClient sharefileclient_in = sharedirectoryclient.CreateFile(filename,1000);
            string filecontent_in = "This is the content of the file.";
            byte[] byteArray = Encoding.UTF8.GetBytes(filecontent_in);
            MemoryStream stream1 = new MemoryStream(byteArray);
            stream1.Position = 0;
            sharefileclient_in.Upload(stream1);

            //read data.
            ShareFileClient sharefileclient_out = sharedirectoryclient.GetFileClient(filename);
            Stream stream2 = sharefileclient_out.Download().Value.Content;
            StreamReader reader = new StreamReader(stream2);
            string filecontent_out = reader.ReadToEnd();

            Console.WriteLine(filecontent_out);
        }
    }
}