|
| 1 | +namespace CZ.Azure.FileExchange.Api; |
| 2 | + |
1 | 3 | using System; |
2 | 4 | using System.Net; |
3 | 5 | using System.Threading.Tasks; |
|
8 | 10 | using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes; |
9 | 11 | using Microsoft.Extensions.Logging; |
10 | 12 | using Microsoft.OpenApi.Models; |
11 | | -using Azure.Storage.Blobs; |
12 | | -using Azure.Storage.Sas; |
| 13 | +using global::Azure.Storage.Blobs; |
| 14 | +using global::Azure.Storage.Sas; |
13 | 15 | using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions; |
14 | 16 | using System.Net.Http; |
15 | 17 |
|
16 | | -namespace CZ.Azure.FileExchange.Api |
| 18 | +public class GenerateSas |
17 | 19 | { |
18 | | - public class GenerateSas |
| 20 | + private readonly ILogger<GenerateSas> logger; |
| 21 | + |
| 22 | + public GenerateSas(ILogger<GenerateSas> log) => |
| 23 | + this.logger = log; |
| 24 | + |
| 25 | + [FunctionName("GenerateSas")] |
| 26 | + [OpenApiOperation(operationId: "Run")] |
| 27 | + [OpenApiParameter(name: "filecode", In = ParameterLocation.Query, Required = false, Type = typeof(string), Description = "The **code** parameter, that represent to get read access to stored files")] |
| 28 | + [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string), Description = "The OK response")] |
| 29 | + public async Task<IActionResult> Run( |
| 30 | + [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req) |
19 | 31 | { |
20 | | - private readonly ILogger<GenerateSas> _logger; |
| 32 | + this.logger.LogInformation("Start generating SaS"); |
21 | 33 |
|
22 | | - public GenerateSas(ILogger<GenerateSas> log) |
| 34 | + var blobservice = new BlobServiceClient(GetEnvironmentVariable("StorageConnectionString")); |
| 35 | + Uri? uri; |
| 36 | + BlobContainerClient? blobContainerClient; |
| 37 | + if (req.Query.TryGetValue("filecode", out var code)) |
| 38 | + { |
| 39 | + blobContainerClient = blobservice.GetBlobContainerClient(code); |
| 40 | + uri = this.GetServiceSasUriForContainer(blobContainerClient, BlobSasPermissions.Read | BlobSasPermissions.List); |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + var response = await blobservice.CreateBlobContainerAsync(Guid.NewGuid().ToString()); |
| 45 | + _ = response.ThrowIfNullOrDefault(); |
| 46 | + blobContainerClient = response.Value; |
| 47 | + uri = this.GetServiceSasUriForContainer(blobContainerClient); |
| 48 | + } |
| 49 | + if (uri == null) |
23 | 50 | { |
24 | | - _logger = log; |
| 51 | + this.logger.LogError("Failed to generate the Sas token"); |
| 52 | + return new BadRequestObjectResult(new StringContent("Failed to greate SaS token to upload your files. Please try again.")); |
25 | 53 | } |
26 | 54 |
|
27 | | - [FunctionName("GenerateSas")] |
28 | | - [OpenApiOperation(operationId: "Run")] |
29 | | - [OpenApiParameter(name: "filecode", In = ParameterLocation.Query, Required = false, Type = typeof(string), Description = "The **code** parameter, that represent to get read access to stored files")] |
30 | | - [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string), Description = "The OK response")] |
31 | | - public async Task<IActionResult> Run( |
32 | | - [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req) |
| 55 | + return new OkObjectResult(uri.ToString()); |
| 56 | + } |
| 57 | + |
| 58 | + private static string GetEnvironmentVariable(string name) => |
| 59 | + Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process) ?? |
| 60 | + throw new ArgumentException($"The setting for {name} is missing."); |
| 61 | + private Uri? GetServiceSasUriForContainer( |
| 62 | + BlobContainerClient containerClient, |
| 63 | + BlobSasPermissions permission = BlobSasPermissions.Write | BlobSasPermissions.Read | BlobSasPermissions.List, |
| 64 | + string? storedPolicyName = null |
| 65 | + ) |
| 66 | + { |
| 67 | + // Check whether this BlobContainerClient object has been authorized with Shared Key. |
| 68 | + if (containerClient.CanGenerateSasUri) |
33 | 69 | { |
34 | | - _logger.LogInformation("Start generating SaS"); |
| 70 | + // Create a SAS token that's valid for one hour. |
| 71 | + var sasBuilder = new BlobSasBuilder() |
| 72 | + { |
| 73 | + BlobContainerName = containerClient.Name, |
| 74 | + Resource = "c" |
| 75 | + }; |
35 | 76 |
|
36 | | - var blobservice = new BlobServiceClient(GetEnvironmentVariable("StorageConnectionString")); |
37 | | - BlobContainerClient? blobContainerClient = null; |
38 | | - Uri uri = null; |
39 | | - if(req.Query.TryGetValue("filecode", out var code)) |
| 77 | + if (storedPolicyName == null) |
40 | 78 | { |
41 | | - blobContainerClient = blobservice.GetBlobContainerClient(code); |
42 | | - uri = GetServiceSasUriForContainer(blobContainerClient, BlobSasPermissions.Read | BlobSasPermissions.List); |
| 79 | + sasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddHours(1); |
| 80 | + sasBuilder.SetPermissions(permission); |
43 | 81 | } |
44 | 82 | else |
45 | 83 | { |
46 | | - var response = await blobservice.CreateBlobContainerAsync(Guid.NewGuid().ToString()); |
47 | | - response.ThrowIfNullOrDefault(); |
48 | | - blobContainerClient = response.Value; |
49 | | - uri = GetServiceSasUriForContainer(blobContainerClient); |
50 | | - } |
51 | | - if (uri == null) |
52 | | - { |
53 | | - _logger.LogError("Failed to generate the Sas token"); |
54 | | - new BadRequestObjectResult(new StringContent("Failed to greate SaS token to upload your files. Please try again.")); |
| 84 | + sasBuilder.Identifier = storedPolicyName; |
55 | 85 | } |
56 | 86 |
|
57 | | - return new OkObjectResult(uri.ToString()); |
58 | | - } |
| 87 | + var sasUri = containerClient.GenerateSasUri(sasBuilder); |
| 88 | + this.logger.LogInformation("SAS URI for blob container is: {0}", sasUri); |
59 | 89 |
|
60 | | - private static string GetEnvironmentVariable(string name) |
61 | | - { |
62 | | - return System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process); |
| 90 | + return sasUri; |
63 | 91 | } |
64 | | - private Uri GetServiceSasUriForContainer( |
65 | | - BlobContainerClient containerClient, |
66 | | - BlobSasPermissions permission = BlobSasPermissions.Write | BlobSasPermissions.Read | BlobSasPermissions.List, |
67 | | - string storedPolicyName = null |
68 | | - ) |
| 92 | + else |
69 | 93 | { |
70 | | - // Check whether this BlobContainerClient object has been authorized with Shared Key. |
71 | | - if (containerClient.CanGenerateSasUri) |
72 | | - { |
73 | | - // Create a SAS token that's valid for one hour. |
74 | | - BlobSasBuilder sasBuilder = new BlobSasBuilder() |
75 | | - { |
76 | | - BlobContainerName = containerClient.Name, |
77 | | - Resource = "c" |
78 | | - }; |
79 | | - |
80 | | - if (storedPolicyName == null) |
81 | | - { |
82 | | - sasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddHours(1); |
83 | | - sasBuilder.SetPermissions(permission); |
84 | | - } |
85 | | - else |
86 | | - { |
87 | | - sasBuilder.Identifier = storedPolicyName; |
88 | | - } |
89 | | - |
90 | | - Uri sasUri = containerClient.GenerateSasUri(sasBuilder); |
91 | | - _logger.LogInformation("SAS URI for blob container is: {0}", sasUri); |
92 | | - |
93 | | - return sasUri; |
94 | | - } |
95 | | - else |
96 | | - { |
97 | | - _logger.LogError(@"BlobContainerClient must be authorized with Shared Key |
| 94 | + this.logger.LogError(@"BlobContainerClient must be authorized with Shared Key |
98 | 95 | credentials to create a service SAS."); |
99 | | - return null; |
100 | | - } |
| 96 | + return null; |
101 | 97 | } |
102 | 98 | } |
103 | 99 | } |
|
0 commit comments