Add project files.
This commit is contained in:
		
							
								
								
									
										95
									
								
								Core.Blueprint.DAL.Infrastrcture/Proxies/ProxyBlobStorage.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								Core.Blueprint.DAL.Infrastrcture/Proxies/ProxyBlobStorage.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,95 @@ | ||||
| using Azure.Storage.Blobs; | ||||
| using Azure.Storage.Blobs.Models; | ||||
| using Azure.Storage.Sas; | ||||
| using Core.Blueprint.DAL.Infrastructure.Contracts; | ||||
| using Core.Blueprint.Domain.Dtos; | ||||
| using Microsoft.Extensions.Azure; | ||||
| using System.Reflection.Metadata; | ||||
|  | ||||
| namespace Core.Blueprint.DAL.Infrastructure.Proxies; | ||||
|  | ||||
| public class ProxyBlobStorage : IProxyBlobStorage | ||||
| { | ||||
|  | ||||
|     private readonly BlobServiceClient _blobServiceClient; | ||||
|     private readonly BlobContainerClient _blobContainerClient; | ||||
|     private const string TIMESTAMP_TAG_NAME = "Timestamp"; | ||||
|     private UserDelegationKey _blobDelegationKey; | ||||
|  | ||||
|     public ProxyBlobStorage(BlobServiceClient blobServiceClient) | ||||
|     { | ||||
|         _blobServiceClient = blobServiceClient; | ||||
|         _blobContainerClient = blobServiceClient.GetBlobContainerClient("blueprint"); | ||||
|         _blobDelegationKey = _blobServiceClient.GetUserDelegationKey(DateTimeOffset.UtcNow, | ||||
|                                                                     DateTimeOffset.UtcNow.AddHours(2)); | ||||
|     } | ||||
|  | ||||
|     public async IAsyncEnumerable<ImageUrlDto> ListAllItemsAsync() | ||||
|     { | ||||
|         await using (var blobs = _blobContainerClient.GetBlobsAsync().GetAsyncEnumerator()) | ||||
|         { | ||||
|             while (await blobs.MoveNextAsync()) | ||||
|             { | ||||
|                 yield return new ImageUrlDto | ||||
|                 { | ||||
|                     Url = _blobContainerClient.GenerateSasUri(BlobContainerSasPermissions.Read, DateTimeOffset.UtcNow.AddMinutes(5)) | ||||
|                 }; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public async Task<ImageUrlDto> GetFirstImageUrlAsync() | ||||
|     { | ||||
|         await using (var blob = _blobContainerClient.GetBlobsAsync().GetAsyncEnumerator()) | ||||
|         { | ||||
|             System.Console.WriteLine(_blobContainerClient.Uri.ToString()); | ||||
|             await blob.MoveNextAsync(); | ||||
|  | ||||
|             var blobClient = _blobContainerClient.GetBlobClient(blob.Current.Name); | ||||
|  | ||||
|             var sasBuilder = new BlobSasBuilder() | ||||
|             { | ||||
|                 BlobContainerName = blobClient.BlobContainerName, | ||||
|                 BlobName = blobClient.Name, | ||||
|                 Resource = "b", | ||||
|                 StartsOn = DateTimeOffset.UtcNow, | ||||
|                 ExpiresOn = DateTimeOffset.UtcNow.AddMinutes(5), | ||||
|             }; | ||||
|             sasBuilder.SetPermissions(BlobAccountSasPermissions.Read); | ||||
|  | ||||
|             var blobUriBuilder = new BlobUriBuilder(blobClient.Uri){ | ||||
|                 Sas = sasBuilder.ToSasQueryParameters(_blobDelegationKey, _blobServiceClient.AccountName) | ||||
|             }; | ||||
|  | ||||
|             var uri = blobUriBuilder.ToUri(); | ||||
|             System.Console.WriteLine(uri); | ||||
|             return new ImageUrlDto { Url = uri }; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public async Task<ImageUrlDto> GetUploadUrl() | ||||
|     { | ||||
|         await using (var blobs = _blobContainerClient.GetBlobsAsync().GetAsyncEnumerator()) | ||||
|         { | ||||
|             await blobs.MoveNextAsync(); | ||||
|  | ||||
|             var blobClient = _blobContainerClient.GetBlobClient(blobs.Current.Name); | ||||
|  | ||||
|             var sasBuilder = new BlobSasBuilder(){ | ||||
|                 BlobContainerName = blobClient.BlobContainerName, | ||||
|                 BlobName = blobClient.Name, | ||||
|                 Resource = "b", | ||||
|                 StartsOn = DateTimeOffset.UtcNow, | ||||
|                 ExpiresOn = DateTimeOffset.UtcNow.AddMinutes(5), | ||||
|             }; | ||||
|             sasBuilder.SetPermissions(BlobAccountSasPermissions.Read); | ||||
|  | ||||
|             var blobUriBuilder = new BlobUriBuilder(blobClient.Uri){ | ||||
|                 Sas = sasBuilder.ToSasQueryParameters(_blobDelegationKey, _blobServiceClient.AccountName) | ||||
|             }; | ||||
|  | ||||
|             var uri = blobUriBuilder.ToUri(); | ||||
|             return new ImageUrlDto { Url = uri }; | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Sergio Matias Urquin
					Sergio Matias Urquin