Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create S3 folder alias or redirect url path to S3 folder

Is it possible to have an alias for a folder in S3 bucket?

Here's what I'm trying to achieve: I want to host a website with documentation of various projects and upload documentation for new versions of these projects to folders in S3 bucket.

Bucket content would be something like this:

<projectA>
|---<0.1.0>
    |---index.html
|---<0.2.0>
    |---index.html    
<projectB>
|---<1.0.0>
    |---index.html
|---<1.1.0>
    |---index.html
index.html

Now I'd like the newest version of any project to be aliased as 'latest'. So when I upload version 1.2.0 of projectB I remove alias 'latest' from version 1.1.0 and add the alias to newly uploaded '1.2.0'.

And url to index page of projectB/1.2.0 would be xxx.cloudfront.net/projectB/latest/index.html

Is something like that even possible? Maybe with some redirects in cloudfront? (or route53)

like image 726
mturek Avatar asked Jul 14 '17 11:07

mturek


People also ask

Can you alias an S3 bucket?

Aliases for S3 Access Points are automatically generated and are interchangeable with S3 bucket names anywhere you use a bucket name for data access. You can use an Access Point alias anywhere a bucket name is used today to perform object-level operations such as PUT, GET, LIST, and more.

How do I create a URL for Amazon S3?

Nothing could be easier, just select all files you want to generate URLs for and click the “Web URL” button on the toolbar. You will see the list of URLs for each of the selected files. Click copy to clipboard button and you are ready to paste URLs to other programs such as an HTML files editor.


1 Answers

Two possible ways with CloudFront.

The easiest is to set up a CloudFront pointing at your bucket, and use the "Origin Path" value. In your example, you set Origin Path to projectB/1.2.0 and call your distribution "latest.mydomain.com". When you update the files, go to origins and change the Origin path.

A more complex version is to use Lambda functions. Hook a lambda function to the "Origin Request" as that is specifically designed to rewrite URLs. https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-at-the-edge.html

like image 152
Robbie Avatar answered Sep 17 '22 20:09

Robbie