无法将文件上传到 blob 存储

时间:2021-04-26 06:13:40

标签: asp.net-core azure-functions azure-blob-storage

我创建了一个 azure 函数应用程序(服务总线触发器),每当我在 UI 中上传 excel 文件时都会触发,如果它无法上传或 excel 文件无效,则它会显示异常的错误。我必须将该异常消息存储到文本文件中的 blob 存储中。 但这并没有发生。

代码:

        string connectionString = "xxxxxxx";
        BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
        string containerName = "collections";
        var fileName = "insurance-collections/2021/exception.txt";
        BlobContainerClient container = blobServiceClient.GetBlobContainerClient(containerName);
        Console.WriteLine("All blobs", container.GetBlobs());
        BlobClient blobClient = container.GetBlobClient(fileName);

异常代码是:

catch (Exception ex)
        {
            logger.LogInformation("Exception " + ex.Message);
            byte[] byteArray = Encoding.UTF8.GetBytes("Exception " + ex.Message + ex.StackTrace);
            MemoryStream stream = new MemoryStream(byteArray);
            blob.Upload(stream);
            Console.WriteLine("Blob uploaded successfully");
        }

请告诉我我做错了什么以及如何使它起作用。

1 个答案:

答案 0 :(得分:0)

这段代码对我来说效果很好,请检查您是否错误地将 blobClient 写成了 blob

catch (Exception ex)
        {
            logger.LogInformation("Exception " + ex.Message);
            byte[] byteArray = Encoding.UTF8.GetBytes("Exception " + ex.Message + ex.StackTrace);
            MemoryStream stream = new MemoryStream(byteArray);
            blobClient.Upload(stream);
            Console.WriteLine("Blob uploaded successfully");
        }

如果您有任何其他问题,请告诉我。