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 HeadBucket 301 Error #5085

Closed
daeho-ro opened this issue Apr 9, 2024 · 3 comments
Closed

S3 HeadBucket 301 Error #5085

daeho-ro opened this issue Apr 9, 2024 · 3 comments
Assignees
Labels
bug This issue is a bug. guidance Question that needs advice or information. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days. service:s3

Comments

@daeho-ro
Copy link

daeho-ro commented Apr 9, 2024

Describe the bug

When I run the head-bucket command from CLI V2 with the bucket located in us-east-1,

> aws s3api head-bucket --bucket <bucket> --region us-east-1     
{
    "BucketRegion": "us-east-1",
    "AccessPointAlias": false
}
> aws s3api head-bucket --bucket <bucket> --region ap-northeast-2
{
    "BucketRegion": "us-east-1",
    "AccessPointAlias": false
}

both region endpoint returns the correct result.

Now, do it the same thing in java sdk v2,

        try (S3Client s3Client = s3ClientBuilder.region(Region.US_EAST_1).build()) {
            region1 = s3Client.headBucket(h -> h.bucket(bucket)).bucketRegion();
            System.out.println(region1);
            // returns us-east-1
        }
        try (S3Client s3Client = s3ClientBuilder.region(Region.AP_NORTHEAST_2).build()) {
            region2 = s3Client.headBucket(h -> h.bucket(bucket)).bucketRegion();
            System.out.println(region2);
            // throws 301 exception
        }

the second block throws an exception and so cannot get the correct region.

How do I get the bucket region info without it? GetBucketLocation is an legacy alternatives but when I read the documentation of it, I noticed that they recommend to move an api to HeadBucket!

We recommend that you use HeadBucket to return the Region that a bucket resides in. For backward compatibility, Amazon S3 continues to support GetBucketLocation.

As far as I know with the error, there is no way to replace the GetBucketLocation by HeadBucket. Is that correct?

Expected Behavior

Wherever I call the HeadBucket, it should return the bucket region info without exception.

Current Behavior

When I call the HeadBucket through the different region endpoint from the bucket region, it throws an exception with the error code 301.

Reproduction Steps

Place a bucket anywhere, and call the HeadBucket.

        try (S3Client s3Client = s3ClientBuilder.region(<where the bucket is not located>).build()) {
            s3Client.headBucket(h -> h.bucket(bucket)).bucketRegion();
            // throws 301 exception
        }

Possible Solution

No response

Additional Information/Context

No response

AWS Java SDK version used

2.25.27

JDK version used

21

Operating System and version

M3 14.3.1

@daeho-ro daeho-ro added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Apr 9, 2024
@bhoradc bhoradc self-assigned this Apr 9, 2024
@bhoradc bhoradc added service:s3 and removed needs-triage This issue or PR still needs to be triaged. labels Apr 9, 2024
@bhoradc
Copy link

bhoradc commented Apr 9, 2024

Hi @daeho-ro,

Thank you for submitting the issue. In this case where you have to use cross-region on the S3 client builder, you should be enabling the cross-region access for it. You may refer to Cross-Region access for Amazon S3 from the developer guide.

By default, the cross-region feature is disabled. To enable it for an S3Client, set crossRegionAccessEnabled to true on the S3Client Builder:

S3Client s3Client = S3Client.builder()
         .region(Region.AP_NORTHEAST_2)
         .crossRegionAccessEnabled(true)
         .build()

Feature available on SDK versions 2.20.111 or higher. Please let me know if you have further questions.

Regards,
Chaitanya

@bhoradc bhoradc added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days. guidance Question that needs advice or information. labels Apr 9, 2024
@daeho-ro
Copy link
Author

daeho-ro commented Apr 9, 2024

Never thought the option lol. It is working good. Thanks!!

@daeho-ro daeho-ro closed this as completed Apr 9, 2024
Copy link

github-actions bot commented Apr 9, 2024

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

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. guidance Question that needs advice or information. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days. service:s3
Projects
None yet
Development

No branches or pull requests

2 participants