Open dscpinheiro opened 7 months ago
In case anyone is inpatient like me, I've created this extension method using reflection to access the HeadBucketAsync() method.
`static class AmazonS3ClientExtension {
static readonly MethodInfo _headBucketAsyncMethod = typeof(AmazonS3Client)
.GetMethod("HeadBucketAsync", BindingFlags.Instance | BindingFlags.NonPublic)!;
static readonly FieldInfo _httpClientResponseDataHeaderField = typeof(HttpClientResponseData)
.GetField("_headers", BindingFlags.NonPublic | BindingFlags.Instance)!;
static public Task<HeadBucketResponse> HeadBucketAsync(this AmazonS3Client client, HeadBucketRequest request, CancellationToken cancellationToken = default)
=> (Task<HeadBucketResponse>)_headBucketAsyncMethod.Invoke(client, [request, cancellationToken])!;
// Use reflection to get the region from the response-headers
static public string? GetRegionFromResponseHeader(this AmazonServiceException ex)
{
var responseEx = ex.InnerException as HttpErrorResponseException;
var responseData = responseEx?.Response as HttpClientResponseData;
var headers = _httpClientResponseDataHeaderField.GetValue(responseData) as Dictionary<string, string>;
string? region = headers?.GetValueOrDefault("x-amz-bucket-region");
return region;
}
}`
Discussed in https://github.com/aws/aws-sdk-net/discussions/3175