Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3 Client not Exposing HeadBucket #3178

Open
dscpinheiro opened this issue Feb 9, 2024 Discussed in #3175 · 1 comment
Open

S3 Client not Exposing HeadBucket #3178

dscpinheiro opened this issue Feb 9, 2024 Discussed in #3175 · 1 comment
Labels
bug This issue is a bug. p2 This is a standard priority issue queued s3

Comments

@dscpinheiro
Copy link
Contributor

Discussed in #3175

Originally posted by omamoo February 7, 2024
Hello all,

I'm writing a code that uploading content to S3 bucket that resides at different account, i.e. I have a cross account scenario of uploading content to S3.

While writing the code I used GetBucketLocation API call to determine the bucket location, however on cross account scenario the GetBucketLocation fails since I'm not the bucket owner, in addition, I saw on AWS documentation that their recommendation is to use HeadBucket API call, however the SDK does not expose this API call and defined this call as internal.

Here is a snapshot from the SDK code:

internal virtual Task<HeadBucketResponse> HeadBucketAsync(HeadBucketRequest request, System.Threading.CancellationToken cancellationToken = default(CancellationToken))
        {
            var options = new InvokeOptions();
            options.RequestMarshaller = HeadBucketRequestMarshaller.Instance;
            options.ResponseUnmarshaller = HeadBucketResponseUnmarshaller.Instance;

            return InvokeAsync<HeadBucketResponse>(request, options, cancellationToken);
        }

Can we make this public for use?

@dscpinheiro dscpinheiro added bug This issue is a bug. needs-review labels Feb 9, 2024
@bhoradc bhoradc added s3 p2 This is a standard priority issue queued and removed needs-review labels Feb 9, 2024
@b-dongen
Copy link

b-dongen commented Sep 2, 2024

In case anyone is inpatient like me, I've created this extension method using reflection to access the HeadBucketAsync() method.

`static class AmazonS3ClientExtension
{
#pragma warning disable S3011 // Reflection should not be used to increase accessibility of classes, methods, or fields
static readonly MethodInfo _headBucketAsyncMethod = typeof(AmazonS3Client)
.GetMethod("HeadBucketAsync", BindingFlags.Instance | BindingFlags.NonPublic)!;
static readonly FieldInfo _httpClientResponseDataHeaderField = typeof(HttpClientResponseData)
.GetField("_headers", BindingFlags.NonPublic | BindingFlags.Instance)!;
#pragma warning restore S3011 // Reflection should not be used to increase accessibility of classes, methods, or fields

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;
}

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. p2 This is a standard priority issue queued s3
Projects
None yet
Development

No branches or pull requests

3 participants