user20358
user20358

Reputation: 14756

How to show the previous version of a static website hosted in S3

I have currently Version 1 and 2 of my static website uploaded into an S3 bucket which has versioning enabled. By default its Version 2 of the website that gets displayed when visitors visit the S3 url. Is there a way to programatically or thru the console set this to display version 1?

Upvotes: 0

Views: 540

Answers (1)

hephalump
hephalump

Reputation: 6164

You access specific versions of a file in S3 by adding a version to your request, like http://example.cloudfront.net/image.png?verisonId=[id].

You’ll also need to set a bucket policy that allows your CloudFront distribution access to the versioned objects:

{
 "Version":"2012-10-17",
 "Id": "CloudFrontAccessToVersionedObjects",
 "Statement": [{
      "Action": ["s3:GetObject", "s3:GetObjectVersion" ],
      "Effect": "Allow",
      "Principal":{"CanonicalUser":"<CLOUDFRONT CANONICAL ID>"},
      "Resource": "<BUCKET>",
      "Sid": "CloudFrontAccess"
 }]}

After applying the bucket policy you’ll need to invalidate the cache.

As a side note, assuming you always want to use a specific version, a quick way to implement this without having to make any changes to your existing code would be to use a Viewer Request Lambda@Edge to rewrite the request, before it’s passed to your S3 Origin, to append the versionId=[id] to all requests to the S3 origin.

Upvotes: 1

Related Questions