Skip to content
Advertisement

How do I set up an AWS S3 Object Redirect via the Java SDK

I’m asking this question in order to answer it and point others in the right direction faster then I got there.

I spent far too long looking for the answer as most S3 Object Redirection questions and their answers had to do with the technical detail of confusing the S3 bucket access point for the S3 website access point.

What, there’s a different url?

Yes — See: https://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteEndpoints.html

Once you get past that the natural path seems to be trying to use ObjectMetaData to set at least one of:

x-amz-website-redirect-location
x-amz-user-website-redirect-location
website-redirect-location

None of which actually work even though the API will silently change these to website-redirect-location when you go check the meta-data through the S3 admin portal.

Of course an S3 Object’s meta data can be easily edited, via the portal, to change the key to Website-Redirect-Location and the redirect instantly starts working, tricking one into thinking that you’ve almost got it right…

The next “obvious” place is to see if there is another method, similar to the methods .setContentLength or .setContentType on ObjectMetaData

Nope that’s not it either.

Advertisement

Answer

The way you actually set the redirect is through the PutObjectRequest itself.

https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/index.html?com/amazonaws/services/s3/model/PutObjectRequest.html

There is a method on that (Java) Object called: .withRedirectLocation which of course accepts the destination URL for the desired redirect.

After far too many hours of searching I stumbled across that little gem here: https://github.com/aws/aws-sdk-go/issues/837

Of course everything is easy once you know how — right?

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement