简体   繁体   English

如何从 VS2019 中的 .NET Core 3.1 控制台应用程序连接到 Azure 文件存储

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

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.我的任务是制作一个 .NET Core 3.1 控制台应用程序,该应用程序将在 AWS 平台上的 linux docker 容器中运行,连接到 Azure 文件存储以读取和写入文件。 I have am a C# programmer but have not had anything to do with the world of containers or Azure as yet.我是一名 C# 程序员,但尚未与容器世界或 Azure 有任何关系。

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我收到了以下格式的 Azure 连接字符串: 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://docs.microsoft.com/en-us/visualstudio/azure/vs-azure-tools-connected-services-storage?view=vs-2019 https://docs.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.在 VS2019 中右键单击项目并添加 Connected Service。
  2. Select Azure Storage from the list.从列表中选择 Azure 存储。
  3. Connect to you Azure Storage account连接到您的 Azure 存储帐户

For step 3 you need to use an Azure Account login email/pass.对于第 3 步,您需要使用 Azure 帐户登录电子邮件/密码。

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/ 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/ https://www.c-sharpcorner.com/article/implement-azure-file-storage-using-asp-net-core-console-application/

But these both use:但这些都使用:

Microsoft.Azure.Storage.Common Microsoft.Azure.Storage.Common

and under dependencies it has .NET Standard and .NET Framework.在依赖项下,它具有 .NET Standard 和 .NET Framework。 I don't think these will run in the linux docker container.我认为这些不会在 linux docker 容器中运行。 Once I have worked out docker containers work I will do a test to confirm this.一旦我解决了 docker 容器的工作,我将进行测试以确认这一点。

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?任何人都可以说明我如何从在 AWS 平台上的 linux docker 容器中运行的 .NET Core 3.1 控制台应用程序连接到 Azure 文件存储以使用上述 Azure 连接字符串格式读取和写入文件?

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.如果你专注于如何在 Connected Service 中添加服务依赖,那么如果你没有 Azure 帐户登录电子邮件/密码,则无法添加服务。

From your description, seems you just want to read and write files from files share of storage using C# console app.根据您的描述,您似乎只想使用 C# 控制台应用程序从存储的文件共享中读取和写入文件。 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);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 防止控制台应用程序 (.NET Core) 打印“exited with code 0”。 在 VS2019 中 - Prevent a Console App (.NET Core) from printing “exited with code 0.” in VS2019 VS2019 WPF.Net Core 3.1 不显示资源字体 - VS2019 WPF .Net Core 3.1 not displaying resource font 如何在通过 VS2019 部署并从 zip 读取的 Azure Function 应用程序中上传文件? - How to upload file in Azure Function app which is deployed through VS2019 and being read from zip? C# 中的 VS2015 到 VS2019 的 Migration.Net core App - Migration .Net core App from VS2015 to VS2019 in C# 从 WPF app Net Core 3.1 连接到 Azure KeyVault - Connect to Azure KeyVault from WPF app Net Core 3.1 如何将我的代码从 VS2019 中的控制台应用程序代码转换为 Windows 窗体 - How do I translate my code from console app code in VS2019 to Windows Form 使用 VS2019 /.Net Core 3.1 的“生成单个文件”发布选项时,是否可以保留 appsettings.json 可编辑的部署后? - Is it possible to keep appsettings.json editable post-deployment when using the “Produce single file” publish option for VS2019 / .Net Core 3.1? 在VS2019中使用.Net Core格式的项目文件时,如何自动增加程序包版本修订号 - How do I autoincrement the Package Version revision number when using a .Net Core format project file in VS2019 vs2019如何运行.xproj文件 - How to run .xproj file in vs2019 尝试从 VS2019 中的控制台 App 调用简单的 POST API - Trying to call simple POST API from console App in VS2019
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM