-
Notifications
You must be signed in to change notification settings - Fork 12
/
AzureBlobStorageHealthCheck.cs
40 lines (35 loc) · 1.53 KB
/
AzureBlobStorageHealthCheck.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
using Azure.Storage.Blobs;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DddDotNet.Infrastructure.Storages.Azure;
public class AzureBlobStorageHealthCheck : IHealthCheck
{
private readonly AzureBlobOption _option;
private readonly BlobContainerClient _container;
public AzureBlobStorageHealthCheck(AzureBlobOption option)
{
_option = option;
_container = new BlobContainerClient(_option.ConnectionString, _option.Container);
}
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default(CancellationToken))
{
try
{
var fileName = _option.Path + $"HealthCheck/{DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss")}-{Guid.NewGuid()}.txt";
using var stream = new MemoryStream(Encoding.UTF8.GetBytes($"HealthCheck {DateTime.Now}"));
await _container.CreateIfNotExistsAsync(cancellationToken: cancellationToken);
BlobClient blob = _container.GetBlobClient(fileName);
await blob.UploadAsync(stream, overwrite: true, cancellationToken);
await blob.DeleteAsync(cancellationToken: cancellationToken);
return HealthCheckResult.Healthy($"ContainerName: {_option.Container}");
}
catch (Exception exception)
{
return new HealthCheckResult(context.Registration.FailureStatus, null, exception);
}
}
}