【问题标题】:Writing to a blob In Azure在 Azure 中写入 blob
【发布时间】:2020-11-07 02:15:14
【问题描述】:

我有以下指向 .Net Core 2.1 的控制台应用程序,它执行从 blob 读取和写入的简单操作。 我在这里用 * 隐藏了帐户密钥。但是连接正常。

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 iscoming from the Program")--line 并注释读取行之后),没有任何内容被写入文件。

注意:文本文件开头为空。首先我尝试通过我的控制台程序写入文件,然后没有发生任何事情然后打开文件并手动写入并能够读取其内容。

当我尝试通过 azure 函数运行相同的代码并将其发布到 azure 门户时,它能够读取/写入文件。

我的问题是我们不能使用 .exe(控制台应用程序)在 azure 中写入 blob?只有通过功能应用程序才能做到? 谁能指导我这里哪里出错了。

【问题讨论】:

    标签: c# azure azure-functions console-application azure-blob-storage


    【解决方案1】:

    因为UploadTextAsync 是一种异步方法,您必须等待它完成(就像您在下载中所做的一样)。请尝试以下代码:

    blob.UploadTextAsync("This is coming from the Program").Result;
    

    或者您可以简单地使用该方法的同步版本:

    blob.UploadText("This is coming from the Program");
    

    【讨论】:

    • 这行得通!但我有一个问题,等待它完成是什么意思?我允许代码完全运行到最后。如何知道异步方法已经结束?我问这个的原因是在运行代码到最后之后,我等了 5 分钟,但仍然没有写入 blob。
    • 当您异步执行方法时,执行发生在后台线程上,您需要等待后台线程完成操作。这就是我所说的waiting for it to finish。更多详情请查看此链接:docs.microsoft.com/en-us/dotnet/csharp/async.
    【解决方案2】:

    这是因为您使用异步进行。如果您使用 Async 执行此操作,那么您需要这样做。

    using Microsoft.Azure.Storage;
    using Microsoft.Azure.Storage.Blob;
    using System;
    using System.Threading.Tasks;
    
    namespace WriteToBlob
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                var writeTask = WriteToBlobAsync("hello world 123");
                writeTask.Wait();
            }
    
            public static async Task WriteToBlobAsync(string text) {
    
                string connectionString = "*";
    
                // 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("test");
    
    
                CloudBlockBlob blob = container.GetBlockBlobReference("test.txt");
                string contents = blob.DownloadTextAsync().Result;
                Console.WriteLine(contents);
                await blob.UploadTextAsync(text);
    
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-30
      • 2019-03-08
      • 2023-01-03
      • 1970-01-01
      • 2023-02-23
      • 2017-12-13
      • 1970-01-01
      相关资源
      最近更新 更多