【问题标题】:How to connect to a Azure File Store from a .NET Core 3.1 console app in VS2019如何在 VS2019 中从 .NET Core 3.1 控制台应用程序连接到 Azure 文件存储
【发布时间】:2020-10-15 04:10:42
【问题描述】:

我的任务是制作一个 .NET Core 3.1 控制台应用程序,该应用程序将在 AWS 平台上的 linux docker 容器中运行,连接到 Azure 文件存储以读取和写入文件。我是一名 C# 程序员,但还没有与容器或 Azure 世界有任何关系。

我收到了以下格式的 Azure 连接字符串: DefaultEndpointsProtocol=https;AccountName=[ACCOUNT_NAME_HERE];AccountKey=[ACCOUNT_KEY_HERE];EndpointSuffix=core.windows.net

但是按照我在网上看到的例子是这样的:

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

  1. 在VS2019中右键项目,添加Connected Service。
  2. 从列表中选择 Azure 存储。
  3. 连接到您的 Azure 存储帐户

对于第 3 步,您需要使用 Azure 帐户登录电子邮件/密码。

我没有那个,我只有一个连接字符串。

我找到了如下示例:

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/

但是这两个都使用:

Microsoft.Azure.Storage.Common

在依赖项下,它具有 .NET Standard 和 .NET Framework。我认为这些不会在 linux docker 容器中运行。一旦我解决了 docker 容器的工作,我将做一个测试来确认这一点。

任何人都可以阐明我如何能够从在 AWS 平台上的 linux docker 容器中运行的 .NET Core 3.1 控制台应用程序连接到 Azure 文件存储,以使用概述的 Azure 连接字符串格式读取和写入文件上面?

【问题讨论】:

标签: c# amazon-web-services docker .net-core azure-storage-files


【解决方案1】:

如果你关注的是如何在Connected Service中添加服务依赖,那么如果你没有Azure账号登录邮箱/密码,就无法添加服务。

根据您的描述,您似乎只想使用 C# 控制台应用程序从存储的文件共享中读取和写入文件。所以你不需要添加项目的服务,只需在你的控制台应用程序中添加代码就可以了。

这是一个简单的代码:

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);
        }
    }
}

【讨论】:

  • 感谢您的回复。您为 Azure 使用了哪些软件包;和 Azure.Storage.Files.Shares?它们是否在带有 .NET Core 的 Linux 上运行?这是我解决的问题之一,很多库都具有 .NET Framework 依赖项,并且无法在具有 .NET Core 的 Linux 上运行。
  • @ScottyLaughton 嗨,我已经做了一个测试。我用 .netcore 创建了一个 linux,上面的代码可以正常工作。你可以试试看。:)
  • 感谢您抽出宝贵时间整理此示例,非常感谢。
猜你喜欢
  • 1970-01-01
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-11
  • 1970-01-01
相关资源
最近更新 更多