Techmag
Techmag

Reputation: 1391

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.

Upvotes: 0

Views: 1259

Answers (2)

searching9x
searching9x

Reputation: 1625

This is a code example, it works for me:

private void highLevelMultipartUploadWithInfo(TransferManager transferManager, //
        String bucketName, String keyName, byte[] fileBytes, 
        long lastModified, String redirectURL) throws Exception {

    String contentType = "text/html";

    InputStream inputStream = new ByteArrayInputStream(fileBytes);

    ObjectMetadata metadata = new ObjectMetadata();

    metadata.setContentType(contentType);
    metadata.setContentLength(fileBytes.length);
    metadata.setLastModified(new java.util.Date(lastModified));

    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, inputStream, metadata)
            .withCannedAcl(CannedAccessControlList.PublicRead) //
            .withRedirectLocation(redirectURL) //
            ;

    // TransferManager processes all transfers asynchronously,
    // so this call returns immediately.
    Upload upload = transferManager.upload(putObjectRequest);
    // Optionally, wait for the upload to finish before continuing.
    upload.waitForCompletion(); 
}

Upvotes: 1

Techmag
Techmag

Reputation: 1391

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?

Upvotes: 0

Related Questions