I am developing HTTP function trigger (2.x) in portal which has POST method. When data POST on the trigger it stores data into file and save that file in Azure File Sharing Storage. For this I am using Azure Storage SDK. However I am getting below error:
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?)
Please refer below code:
#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");
}
Second update:
In this update I will show the whole steps about how to update files to azure successfully,
1.fist of all, you need to build the place where the file will be saved in:
The above is the internal situation of my file storage.
Convenient for you to understand, I will introduce you to the structure of File Storage.
Azure Storage Account: A Storage Account is a namespace used to manage Azure Storage and is used to control access and billing of stored data. Access control for Azure-provided storage services such as Blob, Queue, File, and Table is done through the Storage Account, so to use File Storage, you need to create your Storage Account first.
Share: Share is the unit that manages shared files. Any files and directories to be shared must belong to a Share. The number of shares under one Storage Account is unlimited, and any number of files can be stored in each Share. But each share can hold up to 5TB of data.
Directory: Unlike Blob Storage, File Storage supports true file directories. You can create a directory as needed.
File: File is the real shared file, each file is up to 1TB.
Now, you see, this is my code:
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");
}
}
}
after I run the function and send it with post method, the file upload to file storage succesfully.
Maybe you want to ask me why I use UploadFromStreamAsync() instead of UploadText(). In fact, I also tried UploadText() and passed the value of the string type to it, but I found that this function does not parse all types of request body of the http message well, if you want your program to be reusable, I suggest you use UploadFromStreamAsync().
First Update:
Another problem I want to tell you is to use fileClient.GetShareReference();
, you should use sharename instead of shareurl.
Original answer:
I try your code, and I got the error.
You can use this:
fileToCreate.UploadTextAsync(requestBody);
Then the problem will disappear. no error
I did something similar to yours. I tried uploaded text, images, and videos at the time. If you still have questions, you can ask me.