-
Notifications
You must be signed in to change notification settings - Fork 0
/
AzureFileDownload.cs
63 lines (52 loc) · 2.08 KB
/
AzureFileDownload.cs
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
52
53
54
55
56
57
58
59
60
61
62
63
using System;
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 Microsoft.Extensions.Configuration;
namespace com.businesscentral
{
public class AzureFileDownload
{
private readonly IAzureStorage _azurestorage;
public AzureFileDownload(IAzureStorage azurestorage)
{
_azurestorage = azurestorage;
}
[FunctionName("AzureFileDownload")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
HttpRequest req,
ILogger log,
ExecutionContext context)
{
log.LogInformation("C# HTTP trigger function processed a request.");
#region Process parameters
var fileName = req.Query["fileName"].ToString();
var contentType = req.Query["contentType"].ToString();
if (String.IsNullOrEmpty(fileName))
{
log.LogInformation("Mandatory parameter omitted.");
return new BadRequestResult();
}
log.LogInformation($"Downloading file {fileName}");
if (String.IsNullOrEmpty(contentType))
contentType = "application/binary";
#endregion
#region Load configuration
var configBuilder = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var config = new ConnectorConfig(configBuilder);
#endregion
#region Download
var outputStream = await _azurestorage.DownloadAsync(config, fileName);
return new FileContentResult(outputStream.ToArray(), contentType);
#endregion
}
}
}