英文:
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 "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 requestBody = await new StreamReader(req.Body).ReadToEndAsync();
// dynamic data = JsonConvert.DeserializeObject(requestBody);
// strFileName = strFileName ?? data?.strFileName;
string StorageAccountName = "xyz";
string StorageKey = "i0PNZ6Ykse7oSSfUzFeA36rQfAv9UZnJ5wybQWh5Jol0NRM4sal4s8B3ipkjvfzcsP8/gnI6A==";`enter code here`
string strShareName = "lables";
//string StorageScheme = "SharedKey";
// string FileEndPoint = string.Format("https://{0}.file.core.windows.net/", 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($"Hello, {strFileName}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
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.
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.WindowsAzure.Storage": "9.3.3"
}
}
}
}
Then in the run.csx use the assembly.
#r "Microsoft.WindowsAzure.Storage"
And the below is my work code.
#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";//req.Query["name"];
string Content ="Hello File";
string StorageAccountName = "my account";
string StorageKey = "my key";
string strShareName = "windows";
//string StorageScheme = "SharedKey";
// string FileEndPoint = string.Format("https://{0}.file.core.windows.net/", 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($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
Hope this could help you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论