在Azure函数中创建文件并写入内容时出现问题。

huangapple go评论117阅读模式
英文:

Issue in creating a file and writing content into it in azure function

问题

我正在编写一个用于在Azure存储上创建和上传文本的Azure函数,但我收到了500内部服务器错误。以下是Azure函数的代码。

#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.File;
using System;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string strFileName = "321rahila.csv"; //req.Query["name"];
    string Content = "Hello File";

    string StorageAccountName = "xyz";
    string StorageKey = "i0PNZ6Ykse7oSSfUzFeA36rQfAv9UZnJ5wybQWh5Jol0NRM4sal4s8B3ipkjvfzcsP8/gnI6A==";
    string strShareName = "lables";

    CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(StorageAccountName, StorageKey), true);
    var fileClient = storageAccount.CreateCloudFileClient();
    var share = fileClient.GetShareReference(strShareName);

    {
        var rootDir = share.GetRootDirectoryReference();
        CloudFile file = rootDir.GetFileReference(strFileName);
        var fileToCreate = rootDir.GetFileReference(strFileName);
        fileToCreate.UploadText(Content);
    }

    return strFileName != null
        ? (ActionResult)new OkObjectResult($"Hello, {strFileName}")
        : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}

我在取消注释fileToCreate.UploadText(Content);这一行时收到错误,而且如果不取消注释,我无法创建文件并上传文本。在Visual Studio上,同样的代码运行正常。

英文:

I am writing an azure function for creating and uploading text on azure storage but I got error 500 Internal server error. Below is my code of the azure function.

<!-- language: c# -->

#r &quot;Newtonsoft.Json&quot;
#r &quot;Microsoft.WindowsAzure.Storage&quot;

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.File;
using System;

public static async Task&lt;IActionResult&gt; Run(HttpRequest req, ILogger log)
{
    log.LogInformation(&quot;C# HTTP trigger function processed a request.&quot;);

    string strFileName = &quot;321rahila.csv&quot;;//req.Query[&quot;name&quot;];
    string Content =&quot;Hello File&quot;;

    //string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
   // dynamic data = JsonConvert.DeserializeObject(requestBody);
   // strFileName = strFileName ?? data?.strFileName;
    string StorageAccountName = &quot;xyz&quot;;
    string StorageKey = &quot;i0PNZ6Ykse7oSSfUzFeA36rQfAv9UZnJ5wybQWh5Jol0NRM4sal4s8B3ipkjvfzcsP8/gnI6A==&quot;;`enter code here`
    string strShareName = &quot;lables&quot;;
        //string StorageScheme = &quot;SharedKey&quot;;
    // string FileEndPoint = string.Format(&quot;https://{0}.file.core.windows.net/&quot;, StorageAccountName);
    CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(StorageAccountName, StorageKey), true);
    var fileClient = storageAccount.CreateCloudFileClient();
    var share = fileClient.GetShareReference(strShareName);
   // if (share.Exists())
    {
       var rootDir = share.GetRootDirectoryReference();
       CloudFile file = rootDir.GetFileReference(strFileName);
       var fileToCreate = rootDir.GetFileReference(strFileName);
       **fileToCreate.UploadText(Content);**
    }

    return strFileName != null
        ? (ActionResult)new OkObjectResult($&quot;Hello, {strFileName}&quot;)
        : new BadRequestObjectResult(&quot;Please pass a name on the query string or in the request body&quot;);
}

I get the error when I uncomment the line fileToCreate.UploadText(Content); and without it I am unable to create a file and upload text into it. The same is working fine on visual studio.

答案1

得分: 1

问题在于Azure门户中的Azure函数找不到Microsoft.WindowsAzure.Storage包。正确的方法是创建一个project.json文件并显式引用NuGet包。下面是我的project.json文件。

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "Microsoft.WindowsAzure.Storage": "9.3.3"
      }
    }
   }
}

然后在run.csx中使用该程序集。

#r "Microsoft.WindowsAzure.Storage"

以下是我的工作代码。

#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.File;
using Microsoft.WindowsAzure.Storage.Auth;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string name = req.Query["name"];

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;

    string strFileName = "321rahila.csv";
    string Content ="Hello File";

    string StorageAccountName = "my account";
    string StorageKey = "my key";
    string strShareName = "windows";
    
    CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(StorageAccountName, StorageKey), true);
    var fileClient = storageAccount.CreateCloudFileClient();
    var share = fileClient.GetShareReference(strShareName);
    
    var rootDir = share.GetRootDirectoryReference();
    CloudFile file = rootDir.GetFileReference(strFileName);
    var fileToCreate = rootDir.GetFileReference(strFileName);
    await fileToCreate.UploadTextAsync(Content); 

    return name != null
        ? (ActionResult)new OkObjectResult($"Hello, {name}")
        : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}

希望这能帮助你。

英文:

The problem is the Azure Function in the portal could not find the Microsoft.WindowsAzure.Storage package. The right way is to create project.json file and reference NuGet package explicitly. The below is my project.json file.

{
  &quot;frameworks&quot;: {
    &quot;net46&quot;:{
      &quot;dependencies&quot;: {
        &quot;Microsoft.WindowsAzure.Storage&quot;: &quot;9.3.3&quot;
      }
    }
   }
}

Then in the run.csx use the assembly.

#r &quot;Microsoft.WindowsAzure.Storage&quot;

And the below is my work code.

#r &quot;Newtonsoft.Json&quot;
#r &quot;Microsoft.WindowsAzure.Storage&quot;

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.File;
using Microsoft.WindowsAzure.Storage.Auth;

public static async Task&lt;IActionResult&gt; Run(HttpRequest req, ILogger log)
{
    log.LogInformation(&quot;C# HTTP trigger function processed a request.&quot;);

    string name = req.Query[&quot;name&quot;];

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;

    string strFileName = &quot;321rahila.csv&quot;;//req.Query[&quot;name&quot;];
    string Content =&quot;Hello File&quot;;


    string StorageAccountName = &quot;my account&quot;;
    string StorageKey = &quot;my key&quot;;
    string strShareName = &quot;windows&quot;;
    //string StorageScheme = &quot;SharedKey&quot;;
    // string FileEndPoint = string.Format(&quot;https://{0}.file.core.windows.net/&quot;, StorageAccountName);
    CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(StorageAccountName, StorageKey), true);
    var fileClient = storageAccount.CreateCloudFileClient();
    var share = fileClient.GetShareReference(strShareName);
    // if (share.Exists())
    {
        var rootDir = share.GetRootDirectoryReference();
        CloudFile file = rootDir.GetFileReference(strFileName);
        var fileToCreate = rootDir.GetFileReference(strFileName);
        await fileToCreate.UploadTextAsync(Content); 
    }

    return name != null
        ? (ActionResult)new OkObjectResult($&quot;Hello, {name}&quot;)
        : new BadRequestObjectResult(&quot;Please pass a name on the query string or in the request body&quot;);
}

Hope this could help you.

huangapple
  • 本文由 发表于 2020年1月3日 16:04:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定