#67 added blob storage service + endpoints for profile picture. WIP
This commit is contained in:
74
src/Infrastructure/AzureBlob/AzureBlobStorageService.cs
Normal file
74
src/Infrastructure/AzureBlob/AzureBlobStorageService.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using Azure.Storage.Blobs;
|
||||
using Azure.Storage.Blobs.Models;
|
||||
using Hutopy.Application.Common.Interfaces;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Hutopy.Infrastructure.AzureBlob;
|
||||
|
||||
public class AzureBlobStorageService : IAzureBlobStorageService
|
||||
{
|
||||
private readonly BlobServiceClient _blobServiceClient;
|
||||
|
||||
public AzureBlobStorageService(IConfiguration configuration)
|
||||
{
|
||||
var connectionString = configuration["Azure-Blob-Connection-String"] ?? "";
|
||||
_blobServiceClient = new BlobServiceClient(connectionString);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Upload a file to microsoft azure blob storage.
|
||||
/// </summary>
|
||||
/// <param name="blobName">The blob name (path within the container, include the file name).</param>
|
||||
/// <param name="containerName">The name of the container where the file is stored.</param>
|
||||
/// <param name="fileStream">The stream.</param>
|
||||
/// <returns></returns>
|
||||
public async Task<string> UploadFileAsync(string containerName, string blobName, Stream fileStream)
|
||||
{
|
||||
// Get a reference to a container
|
||||
var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
|
||||
|
||||
// Create the container if it does not exist
|
||||
await containerClient.CreateIfNotExistsAsync();
|
||||
|
||||
// Get a reference to a blob
|
||||
var blobClient = containerClient.GetBlobClient(blobName);
|
||||
|
||||
// Upload the file
|
||||
await blobClient.UploadAsync(fileStream, true);
|
||||
|
||||
// Return the URI of the uploaded blob
|
||||
return blobClient.Uri.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Download a file to microsoft azure blob storage.
|
||||
/// </summary>
|
||||
/// <param name="blobName">The blob name (path within the container).</param>
|
||||
/// <param name="containerName">The name of the container where the file is stored. (users)</param>
|
||||
/// <returns></returns>
|
||||
public async Task<MemoryStream> DownloadFileAsync(string containerName, string blobName)
|
||||
{
|
||||
// Get a reference to a container
|
||||
var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
|
||||
|
||||
// Get a reference to a blob
|
||||
var blobClient = containerClient.GetBlobClient(blobName);
|
||||
|
||||
try
|
||||
{
|
||||
// Download the blob to a stream
|
||||
BlobDownloadInfo download = await blobClient.DownloadAsync();
|
||||
MemoryStream memoryStream = new();
|
||||
await download.Content.CopyToAsync(memoryStream);
|
||||
memoryStream.Position = 0; // Ensure the stream is at the beginning
|
||||
|
||||
return memoryStream;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Log and handle the exception as needed
|
||||
throw new ApplicationException("Error downloading file from Azure Blob Storage.", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
33
src/Infrastructure/AzureBlob/BlobStructure.txt
Normal file
33
src/Infrastructure/AzureBlob/BlobStructure.txt
Normal file
@@ -0,0 +1,33 @@
|
||||
users/
|
||||
│
|
||||
├── userId1/
|
||||
│ ├── profile/
|
||||
│ │ └── profilePicture.jpg
|
||||
│ │ └── data.json
|
||||
│ │
|
||||
│ ├── posts/
|
||||
│ │ ├── post1/
|
||||
│ │ │ ├── image1.jpg
|
||||
│ │ │ ├── video1.mp4
|
||||
│ │ │ └── audio1.mp3
|
||||
│ │ ├── post2/
|
||||
│ │ │ ├── image2.jpg
|
||||
│ │ │ └── video2.mp4
|
||||
│ │ └── ...
|
||||
│
|
||||
├── userId2/
|
||||
│ ├── profile/
|
||||
│ │ └── profilePicture.jpg
|
||||
│ │ └── data.json
|
||||
│ │
|
||||
│ ├── posts/
|
||||
│ │ ├── post1/
|
||||
│ │ │ ├── image1.jpg
|
||||
│ │ │ ├── video1.mp4
|
||||
│ │ │ └── audio1.mp3
|
||||
│ │ ├── post2/
|
||||
│ │ │ ├── image2.jpg
|
||||
│ │ │ └── video2.mp4
|
||||
│ │ └── ...
|
||||
│
|
||||
└── ...
|
||||
@@ -1,5 +1,6 @@
|
||||
using Hutopy.Application.Common.Interfaces;
|
||||
using Hutopy.Domain.Constants;
|
||||
using Hutopy.Infrastructure.AzureBlob;
|
||||
using Hutopy.Infrastructure.Data;
|
||||
using Hutopy.Infrastructure.Data.Interceptors;
|
||||
using Hutopy.Infrastructure.Identity;
|
||||
@@ -63,8 +64,14 @@ public static class DependencyInjection
|
||||
.AddSignInManager<SignInManager<ApplicationUser>>()
|
||||
.AddDefaultTokenProviders();
|
||||
|
||||
// Singleton services
|
||||
services.AddSingleton(TimeProvider.System);
|
||||
services.AddSingleton<IAzureBlobStorageService, AzureBlobStorageService>();
|
||||
|
||||
// Scoped services
|
||||
services.AddScoped<IIdentityService, IdentityService>();
|
||||
|
||||
// Transient services
|
||||
services.AddTransient<IStripeService, StripeService>();
|
||||
|
||||
services.AddAuthorization(options =>
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
<AssemblyName>Hutopy.Infrastructure</AssemblyName>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Azure.Storage.Blobs" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" />
|
||||
|
||||
Reference in New Issue
Block a user