From 51ca0ce7dbd775a95c54fde0d93f8cfc54e7f365 Mon Sep 17 00:00:00 2001 From: peggy-quartech Date: Fri, 5 Sep 2025 22:30:34 -0700 Subject: [PATCH 1/2] fix --- src/Spd.Utilities.Hosting/S3V2HealthCheck.cs | 39 +++++++++++++++++++ .../ServiceExtensionsHealthChecks.cs | 37 +++++++++++------- 2 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 src/Spd.Utilities.Hosting/S3V2HealthCheck.cs diff --git a/src/Spd.Utilities.Hosting/S3V2HealthCheck.cs b/src/Spd.Utilities.Hosting/S3V2HealthCheck.cs new file mode 100644 index 0000000000..1fce692d4c --- /dev/null +++ b/src/Spd.Utilities.Hosting/S3V2HealthCheck.cs @@ -0,0 +1,39 @@ +using Amazon.S3; +using Amazon.S3.Model; +using Microsoft.Extensions.Diagnostics.HealthChecks; + +namespace Spd.Utilities.Hosting; +public class S3V2HealthCheck : IHealthCheck +{ + private readonly IAmazonS3 _s3Client; + private readonly string _bucketName; + + public S3V2HealthCheck(IAmazonS3 s3Client, string bucketName) + { + _s3Client = s3Client; + _bucketName = bucketName; + } + + public async Task CheckHealthAsync( + HealthCheckContext context, + CancellationToken cancellationToken = default) + { + try + { + var request = new ListObjectsV2Request + { + BucketName = _bucketName, + MaxKeys = 1 + }; + + await _s3Client.ListObjectsV2Async(request, cancellationToken); + + return HealthCheckResult.Healthy("S3 bucket is reachable."); + } + catch (Exception ex) + { + return HealthCheckResult.Unhealthy("S3 health check failed.", ex); + } + } +} + diff --git a/src/Spd.Utilities.Hosting/ServiceExtensionsHealthChecks.cs b/src/Spd.Utilities.Hosting/ServiceExtensionsHealthChecks.cs index 3cbc80079f..f912ca9780 100644 --- a/src/Spd.Utilities.Hosting/ServiceExtensionsHealthChecks.cs +++ b/src/Spd.Utilities.Hosting/ServiceExtensionsHealthChecks.cs @@ -29,25 +29,34 @@ public static IServiceCollection AddHealthChecks(this IServiceCollection service healthCheckBuilder.AddRedis(redisConnection); } + //after awssdk upgrade to 4.0.6.8, AspNetCore.HealthChecks.Aws.S3 does not upgrade accordingly, + //The AddS3 health check extension internally uses the older ListObjectsRequest API to check connectivity. + //In newer versions of the AWS SDK, ListObjectsRequest.MaxKeys has been deprecated or removed, replaced by ListObjectsV2Request + //When your runtime uses a newer AWSSDK.S3 version, that setter no longer exists, triggering the MissingMethodException. + //We have to Override with a custom health check + // Replacement for AddS3 (uses ListObjectsV2) var s3url = configuration.GetValue("storage:MainBucketSettings:url"); - if (s3url != null) + var bucket = configuration.GetValue("storage:MainBucketSettings:bucket"); + var accessKey = configuration.GetValue("storage:MainBucketSettings:accessKey"); + var secretKey = configuration.GetValue("storage:MainBucketSettings:secret"); + + if (!string.IsNullOrEmpty(s3url) && !string.IsNullOrEmpty(bucket)) { - healthCheckBuilder.AddS3(options => + services.AddSingleton(sp => { - options.S3Config = new AmazonS3Config - { - ServiceURL = s3url, - ForcePathStyle = true, - UseHttp = false, - }; - - options.BucketName = configuration.GetValue("storage:MainBucketSettings:bucket", string.Empty)!; - options.Credentials = new BasicAWSCredentials( - configuration.GetValue("storage:MainBucketSettings:accessKey", string.Empty), - configuration.GetValue("storage:MainBucketSettings:secret", string.Empty)); + var s3Client = new AmazonS3Client( + new BasicAWSCredentials(accessKey, secretKey), + new AmazonS3Config + { + ServiceURL = s3url, + ForcePathStyle = true, + UseHttp = false + }); + return new S3V2HealthCheck(s3Client, bucket); }); - } + healthCheckBuilder.AddCheck("aws_s3_v2"); + } return services; } From 186cf7a6ae8540af7965af32dae4d5600df05728 Mon Sep 17 00:00:00 2001 From: peggy-quartech Date: Mon, 8 Sep 2025 09:32:37 -0700 Subject: [PATCH 2/2] upgrade s3 health check --- src/Spd.Utilities.Hosting/ServiceExtensionsHealthChecks.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Spd.Utilities.Hosting/ServiceExtensionsHealthChecks.cs b/src/Spd.Utilities.Hosting/ServiceExtensionsHealthChecks.cs index f912ca9780..fc9f75acce 100644 --- a/src/Spd.Utilities.Hosting/ServiceExtensionsHealthChecks.cs +++ b/src/Spd.Utilities.Hosting/ServiceExtensionsHealthChecks.cs @@ -42,7 +42,7 @@ public static IServiceCollection AddHealthChecks(this IServiceCollection service if (!string.IsNullOrEmpty(s3url) && !string.IsNullOrEmpty(bucket)) { - services.AddSingleton(sp => + services.AddSingleton(sp => { var s3Client = new AmazonS3Client( new BasicAWSCredentials(accessKey, secretKey), @@ -54,8 +54,6 @@ public static IServiceCollection AddHealthChecks(this IServiceCollection service }); return new S3V2HealthCheck(s3Client, bucket); }); - - healthCheckBuilder.AddCheck("aws_s3_v2"); } return services; }