使用 Azure 存储 SDK 尝试写入文件并将其上传到 Azure 文件共享存储时出错

Getting error while trying to write in file and upload it to azure file sharing storage using Azure Storage SDK

我正在开发具有 POST 方法的门户中的 HTTP 函数触发器 (2.x)。当触发器上的数据 POST 时,它会将数据存储到文件中,并将该文件保存在 Azure 文件共享存储中。为此,我使用 Azure 存储 SDK。但是我遇到以下错误:

error CS1061: 'CloudFile' does not contain a definition for 'UploadText' and no extension method 'UploadText' accepting a first argument of type 'CloudFile' could be found (are you missing a using directive or an assembly reference?)

请参考以下代码:

#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
using System.Net;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.File;
using System.IO;

 public static async Task<IActionResult> Run(HttpRequest req)
    {
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

        var storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=xxxxxxxxxxxxxxxxx;AccountKey=xxxxxx==;FileEndpoint=https://xxxxxxxxxxxxxxxxx.file.core.windows.net/;");
        var fileClient = storageAccount.CreateCloudFileClient();
        string baseShareName = "https://xxxxxxxxxxxxxxxxx.file.core.windows.net/xxxxxxxxxxxxxxxxx921d";
        var share = fileClient.GetShareReference(baseShareName);
        var rootDir = share.GetRootDirectoryReference();  
        var sampleDir = rootDir.GetDirectoryReference("WorkingFolder");
        var fileToCreate = sampleDir.GetFileReference("output.txt");

        fileToCreate.UploadText(requestBody);

        return new OkObjectResult("Data has been received");
    }

第二次更新:

在本次更新中,我将展示如何成功将文件更新到 Azure 的整个步骤,

1.fist 总而言之,您需要构建文件保存位置: 以上是我的文件存储内部情况。

方便大家理解,我给大家介绍一下File Storage的结构。

Azure 存储帐户: 存储帐户是用于管理 Azure 存储的命名空间,用于控制存储数据的访问和计费。 Azure 提供的存储服务,如 Blob、Queue、File 和 Table 的访问控制是通过存储帐户完成的,因此要使用文件存储,您需要先创建存储帐户。

分享: Share是管理共享文件的单位。任何要共享的文件和目录都必须属于共享。一个 Storage Account 下的 Share 数量没有限制,每个 Share 可以存储任意数量的文件。但每个共享最多可容纳 5TB 的数据。

目录: 与 Blob 存储不同,文件存储支持真正的文件目录。您可以根据需要创建目录。

文件: 文件是真正的共享文件,每个文件最大1TB。

现在,你看,这是我的代码:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage;

namespace UploadFileFunction
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {

            var storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=bowmanimagestorage02;AccountKey=xxxxxxxx==;BlobEndpoint=https://bowmanimagestorage02.blob.core.windows.net/;TableEndpoint=https://bowmanimagestorage02.table.core.windows.net/;QueueEndpoint=https://bowmanimagestorage02.queue.core.windows.net/;FileEndpoint=https://bowmanimagestorage02.file.core.windows.net");
            var myClient = storageAccount.CreateCloudFileClient();
            var share = myClient.GetShareReference("testfileshare");
            var rootDir = share.GetRootDirectoryReference();
            var sampleDir = rootDir.GetDirectoryReference("WorkingFolder");
            var fileToCreate = sampleDir.GetFileReference("output.txt");

            await fileToCreate.UploadFromStreamAsync(req.Body);

            return new OkObjectResult("Data has been received");
        }
    }
}

这是我的 txt 文件:

在我运行函数并用post方法发送后,文件上传到文件存储成功。

也许您想问我为什么使用 UploadFromStreamAsync() 而不是 UploadText()。其实我也试过UploadText(),把string类型的值传给它,但是我发现这个函数并没有很好的解析http报文的所有类型的request body,如果你希望你的程序可以复用,我建议您使用 UploadFromStreamAsync()。


第一次更新:

我要告诉你的另一个问题是使用fileClient.GetShareReference();,你应该使用sharename而不是shareurl。


原答案:

我试了你的代码,我得到了 error。

你可以使用这个:

fileToCreate.UploadTextAsync(requestBody);

那么问题就会消失。 no error

我做了和你类似的事情。我当时尝试上传文字、图片和视频。如果还有问题可以问我