我有以下指向 .Net Core 2.1 的控制台应用程序,它执行从 blob 读取和写入的简单操作。 我在这里用 * 隐藏了帐户 key 。但连接正常。
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string connectionString = $"DefaultEndpointsProtocol=https;AccountName=r53eripcjroswtest;AccountKey=***;";
// Setup the connection to the storage accounts
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
// Connect to the blob storage
CloudBlobClient serviceClient = storageAccount.CreateCloudBlobClient();
// Connect to the blob container
CloudBlobContainer container = serviceClient.GetContainerReference("igb-test");
CloudBlockBlob blob = container.GetBlockBlobReference("Test.txt");
string contents = blob.DownloadTextAsync().Result;
//blob.UploadTextAsync("This is coming from the Program");
}
}
}
我已经运行了上面的代码,并且能够读取“contents”变量中的文件内容。 但是当我尝试写入文件时(取消注释 ---blob.UploadTextAsync("This is come from the Program")--line 并注释读取行后),没有任何内容被写入文件。
注意:文本文件一开始是空的。首先,我尝试通过控制台程序写入文件,但没有发生任何事情,然后打开文件并手动写入并能够读取其内容。
当我尝试通过 azure 函数运行相同的代码并将其发布到 azure 门户时,它能够读取和写入文件。
我的问题是我们不能使用 .exe(控制台应用程序)写入 azure 中的 blob?只有通过功能应用程序才能完成吗? 任何人都可以指导我这里出错的地方吗?
最佳答案
因为 UploadTextAsync
是一个异步方法,因此您必须等待它完成(就像您在下载中所做的那样)。请尝试以下代码:
blob.UploadTextAsync("This is coming from the Program").Result;
或者您可以简单地使用该方法的同步版本:
blob.UploadText("This is coming from the Program");
关于c# - 写入 Azure 中的 blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62951158/