如何从 VS2019 中的 .NET Core 3.1 控制台应用程序连接到 Azure 文件存储
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
但是按照我在网上看到的例子是这样的:
- 右键单击VS2019中的项目并添加连接服务。
- Select Azure 存储来自列表。
- 连接到您的 Azure 存储帐户
对于第 3 步,您需要使用 Azure 帐户登录 email/pass。
我没有,我只有一个连接字符串。
我找到了如下示例:
但这两个都使用:
Microsoft.Azure.Storage.Common
并且在依赖项下它有 .NET Standard 和 .NET Framework。我认为这些不会 运行 在 linux docker 容器中。一旦我计算出 docker 容器工作,我将进行测试以确认这一点。
任何人都可以阐明我如何从 AWS 平台上 linux docker 容器中的 .NET Core 3.1 控制台应用程序 运行 连接到Azure 文件存储使用上面概述的 Azure 连接字符串格式读取和写入文件?
如果您专注于如何在连接服务中添加服务依赖项,那么如果您没有 Azure 帐户登录 email/pass 则无法添加该服务。
根据您的描述,您似乎只想使用 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);
}
}
}
我的任务是制作一个 .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
但是按照我在网上看到的例子是这样的:
- 右键单击VS2019中的项目并添加连接服务。
- Select Azure 存储来自列表。
- 连接到您的 Azure 存储帐户
对于第 3 步,您需要使用 Azure 帐户登录 email/pass。
我没有,我只有一个连接字符串。
我找到了如下示例:
但这两个都使用:
Microsoft.Azure.Storage.Common
并且在依赖项下它有 .NET Standard 和 .NET Framework。我认为这些不会 运行 在 linux docker 容器中。一旦我计算出 docker 容器工作,我将进行测试以确认这一点。
任何人都可以阐明我如何从 AWS 平台上 linux docker 容器中的 .NET Core 3.1 控制台应用程序 运行 连接到Azure 文件存储使用上面概述的 Azure 连接字符串格式读取和写入文件?
如果您专注于如何在连接服务中添加服务依赖项,那么如果您没有 Azure 帐户登录 email/pass 则无法添加该服务。
根据您的描述,您似乎只想使用 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);
}
}
}