Files stored in AWS S3 can be redirected to specific URLs. By utilizing the S3 static website feature, a special header tag “x-amz-website-redirect-location” added to the file stored in S3 is interpreted by the static S3 Website as the HTTP 301 redirect and handled as such. Lets see how it works.
In AWS language, this is called “Redirect requests for an object“. Objects in most all cases are files in S3, except in this case it is merely a placeholder where we set attributes to tell the S3 Website how to redirect the path we are visiting to a new URL.
One important detail to note, S3 Redirect requests for objects cannot redirect folder object paths, or paths that end with a forward slash. In other words, as long as the URLs end without a slash you can use this method, otherwise you will need to use one of the other redirection methods.
Setup CloudFront with S3 Static Website
To get started, you first need to setup S3 with the S3 Static Website feature, then setup CloudFront in front of your S3 website. Please see URL Redirects on Amazon Web Services with CloudFront and S3 for details how to setup CloudFront with S3 and S3 Website.
Redirect a file in a S3 bucket via AWS ClI
For this example we upload a nearly empty file as a placeholder. Go ahead and create a file with your favorite editor and put “redirect” in the file then save it. I recommend saving the file as the same filename of the path you want to redirect.
We can easily write a file with a very simple body value and include the new website location in one simple command line. First, create a near empty file on your computer to act as a placeholder for the file to be redirected in the S3 bucket. I like to make this a plain text file with the line “redirect” that way if someone else someday looks at the file they can hopefully quickly figure out what it is for. This will be specified in our example with the –body argument. If you can save it in the current working directory then you will not need to specify a path to the file, just the filename itself.
I assume you already know how to use the AWS command line tool to proceed. You should already have the command line tool configured with a key and secret key as well as have the default region set where your bucket resides. This example assumes you are using the “default” profile. Remember to add –profile NAME if you wish to use a different profile.
aws s3api put-object \ --bucket bucket-name-here \ --acl public-read \ --key path/in/s3/bucket/file.ext \ --body path/to/local/file \ --website-redirect-location https://example.com/path/to/file
Learn more about the AWS CLI and parameters on the AWS CLI put-Object documentation page.
Redirect a file in a S3 bucket via NodeJS
With NodeJS, the process is quite easy. We start by making a putObject call and include a parameter “WebsiteRedirectLocation” set to the value of where we want to redirect to. Note that in this example we are setting a very simple “Body” value. The body could be blank as the public will not see it. Putting something simple in the file may save questions later.
const params = { ACL: "public-read", WebsiteRedirectLocation: "https://example.com/path/to/file", Bucket: "bucket-name-here", Key: "path/in/s3/bucket/file.ext", ContentType: 'text/html', Body: "<h1>redirected!</h1>" } try { const s3Response = await s3.putObject(params).promise(); } catch (err) { console.log(err) }
The example above is synchronous. Depending on your situation you may be updating many paths in an S3 bucket, in that case you would want to gather the promises and then wait for them all to return. That would look something like this…
var promises = []; while( ... ) { const params = { ACL: "public-read", WebsiteRedirectLocation: "https://example.com/path/to/file", Bucket: "bucket-name-here", Key: "path/in/s3/bucket/file.ext", ContentType: 'text/html', Body: "<h1>redirected!</h1>" } try { const s3ResponsePromise = s3.putObject(params).promise(); promises.push(s3ResponsePromise); } catch (err) { console.log(err) } } Promise.all(promises).then(results => { console.log(results); });
Redirecting URLs in AWS Conclusion
As we have learned, there are a number of ways to redirect URLs with AWS in a serverless and cost effective way by using S3 and the S3 Website feature. For a recap, please check out the articles that complement this one:
- URL Redirection on AWS with CloudFront and S3
- Redirect Entire hostname on AWS using a S3 Website
- URL Redirection on AWS using JSON rules
Please leave suggestions, comments and feedback below, it is greatly appreciated!