![](/img/trans.png)
[英]Prevent a Console App (.NET Core) from printing “exited with code 0.” in VS2019
[英]How to connect to a Azure File Store from a .NET Core 3.1 console app in VS2019
我的任务是制作一个 .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
但是按照我在网上看到的例子是这样的:
对于第 3 步,您需要使用 Azure 帐户登录电子邮件/密码。
我没有那个,我只有一个连接字符串。
我找到了如下示例:
但这些都使用:
Microsoft.Azure.Storage.Common
在依赖项下,它具有 .NET Standard 和 .NET Framework。 我认为这些不会在 linux docker 容器中运行。 一旦我解决了 docker 容器的工作,我将进行测试以确认这一点。
任何人都可以说明我如何从在 AWS 平台上的 linux docker 容器中运行的 .NET Core 3.1 控制台应用程序连接到 Azure 文件存储以使用上述 Azure 连接字符串格式读取和写入文件?
如果你专注于如何在 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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.