简体   繁体   English

如何在 Azure Function (.NET6) 中读取和写入相同的 Blob 文件

[英]How to Read & Write to same Blob file in Azure Function (.NET6)

In Azure Function V1 (.NET4) code below worked fine在 Azure Function V1 (.NET4) 下面的代码运行良好

[FunctionName("run")]
public static HttpResponseMessage run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage request,
[Blob("sample.txt", FileAccess.Read)] Stream readStream,
[Blob("sample.txt", FileAccess.Write)] Stream writeStream) 
{
   //read & write to sample.txt stream works ok
}

But in Azure Function V4 (.NET 6), throws error that only 1 stream can be accessed但是在 Azure Function V4 (.NET 6) 中,抛出只能访问 1 stream 的错误

[FunctionName("run")]
public static HttpResponseMessage run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage request,
[Blob("sample.txt", FileAccess.Read)] Stream readStream,
[Blob("sample.txt", FileAccess.Write)] Stream writeStream) 
{
   //can ONLY use read stream or write stream, can't use both

}

How to Read & Write to same Blob file in Azure Function (.NET6)?如何读取和写入 Azure Function (.NET6) 中的同一个 Blob 文件?

Note: issue only appears in Azure not in local debugging注意:问题只出现在 Azure 不在本地调试

After reproducing from our end, This was working even though we have added 2 streams.从我们这边重现后,即使我们添加了 2 个流,它也能正常工作。 Make sure your environment is up to date.确保您的环境是最新的。

[FunctionName("Function1")]
        public static void Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req,
        [Blob("samples-workitems/samples.txt", FileAccess.Read)] Stream readStream,
        [Blob("samples-workitems/samples.txt", FileAccess.Write)] Stream writeStream)
        {         
            Console.WriteLine("Blob length is "+ readStream.Length);

            string sample = "Sample Text";

            byte[] bytes = Encoding.ASCII.GetBytes(sample);
            writeStream.Write(bytes, 0, bytes.Length);

        }

Considering this to be my initial text file考虑这是我的初始文本文件

在此处输入图像描述

RESULT:结果:

在此处输入图像描述

Text file after post-trigger后触发后的文本文件

在此处输入图像描述

Final solution that worked in Azure Function V4 (.NET 6) is BlobClient在 Azure Function V4 (.NET 6) 中工作的最终解决方案是BlobClient

Code to use BlobClient to read/write files in blob storage使用BlobClient读取/写入 blob 存储中的文件的代码

[FunctionName("Function1")]
public static async Task<IActionResult> Function1(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage incomingRequest,
[Blob("container-name/file-name.xml", FileAccess.ReadWrite)] BlobClient fileBlobClient)
    { ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Powershell Azure Function 将 Excel 文件写入 Azure Blob 存储 - Powershell Azure Function to write an Excel file to Azure Blob Storage Azure Function(.NET 7 隔离)创建一个 Azure Function 应用程序针对 .NET6? - Azure Function (.NET 7 Isolated) create an Azure Function App targeting .NET6? Azure Blob 存储 - 如何读取源字符串:wasbs://training@dbtrainsouthcentralus.blob.core.windows.net - Azure Blob Storage - How to read source string: wasbs://training@dbtrainsouthcentralus.blob.core.windows.net 从 azure function 服务的 blob 存储中读取 React 应用程序上的 blob 音频文件 - Read blob audio file on react app from blob storage served by azure function .NET6 隔离 Azure Function 单元和集成测试导致 gRPC 异常 - .NET6 Isolated Azure Function Unit and Integration Test causes gRPC exception 如何将 Azure 认知服务语音音频 output 写入 Azure Blob? - How to write Azure Cognitive Services Speech audio output to an Azure Blob? 通过 Azure SignalR 连接 .net6 网络服务和 .net6 wpf 应用程序 - Connect .net6 web-service and .net6 wpf application via Azure SignalR 如何从 azure blob 存储中读取“.owl” - How to read an ".owl" from azure blob storage 是否可以从 Azure function 所在的同一文件夹中读取文件 - Is it possible to read File from same folder where Azure function exists Azure AD B2C 和常规 (SQL) Identity.Net6 - Azure AD B2C and regular (SQL) Identity .Net6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM