-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpTriggerFileToBlob.cs
More file actions
51 lines (46 loc) · 1.57 KB
/
Copy pathHttpTriggerFileToBlob.cs
File metadata and controls
51 lines (46 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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.Azure.Storage.Blob;
namespace BlobInput
{
public static class HttpTriggerFileToBlob
{
[FunctionName("HttpTriggerFileToBlob")]
public static async Task Run
(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
[Blob("blobtest/httptrigger.txt",FileAccess.Write,Connection = "AzureWebJobsStorage")] Stream blob,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
try
{
var formdata = await req.ReadFormAsync();
var files = formdata.Files["file"];
var fileBytes = await GetBytes(files);
blob.Write(fileBytes, 0, fileBytes.Length);
log.LogInformation("File successfully uploaded to blob");
}
catch (System.Exception)
{
log.LogInformation("File failed to upload to blob");
throw;
}
}
public static async Task<byte[]> GetBytes(this IFormFile formFile)
{
using (var memoryStream = new MemoryStream())
{
await formFile.CopyToAsync(memoryStream);
return memoryStream.ToArray();
}
}
}
}