-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e52be9
commit 9b6992b
Showing
11 changed files
with
98 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 4 additions & 26 deletions
30
MangoAPI.BusinessLogic/DependencyInjection/AzureBlobInitializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,20 @@ | ||
using Azure.Storage.Blobs; | ||
using Azure.Storage.Blobs.Models; | ||
using MangoAPI.Application.Interfaces; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.IO; | ||
|
||
namespace MangoAPI.BusinessLogic.DependencyInjection; | ||
|
||
public static class AzureBlobInitializer | ||
{ | ||
private const string SeedImagesFolder = "../../../../img/seed_images"; | ||
|
||
public static void InitializeAzureBlob(this IApplicationBuilder app) | ||
{ | ||
using var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>() | ||
.CreateScope(); | ||
|
||
var blobClient = serviceScope.ServiceProvider.GetService<BlobServiceClient>(); | ||
var blobServiceSettings = serviceScope.ServiceProvider.GetService<IBlobServiceSettings>(); | ||
|
||
var containerClient = blobClient.GetBlobContainerClient(blobServiceSettings.MangoBlobContainerName); | ||
containerClient.CreateIfNotExists(); | ||
containerClient.SetAccessPolicy(PublicAccessType.BlobContainer); | ||
|
||
var files = Directory.GetFiles(Path.Combine(AppContext.BaseDirectory, "../../../../img/seed_images")); | ||
foreach (var filePath in files) | ||
{ | ||
var fileName = filePath.Split(@"\")[^1]; | ||
var client = containerClient.GetBlobClient(fileName); | ||
var result = client.Exists(); | ||
if (result.Value) | ||
{ | ||
continue; | ||
} | ||
|
||
using var stream = File.OpenRead(filePath); | ||
var blobService = serviceScope.ServiceProvider.GetService<IBlobService>(); | ||
|
||
var headers = new BlobHttpHeaders { ContentType = "image/jpg" }; | ||
client.Upload(stream, headers); | ||
} | ||
blobService.UploadFolderToBlob(SeedImagesFolder); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters