Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudfront: setting s3 path difference from Origin

Cache behaviour setting, i use

s3/*

to redirect request to s3 folder, but i don't want to place my images under a folder named s3 but right under s3 bucket to redirect like this:

xxxx.cloudfront.com/s3/images/1.png  -> bucket_name/images/1.png

not

xxxx.cloudfront.com/s3/images/1.png  -> bucket_name/s3/images/1.png

Please help to show me how to config like this.

like image 343
Cụt Cánh Avatar asked Oct 12 '16 08:10

Cụt Cánh


People also ask

What is CloudFront origin path?

Origin path CloudFront appends the directory path to the value of Origin domain, for example, cf-origin.example.com/production/images .

Can CloudFront have multiple S3 origins?

You can configure a single CloudFront web distribution to serve different types of requests from multiple origins. For example, your website might serve static content from an Amazon Simple Storage Service (Amazon S3) bucket and dynamic content from a load balancer.

What is difference between edge location and CloudFront?

CloudFront delivers your content through a worldwide network of data centers called edge locations. When a user requests content that you're serving with CloudFront, the request is routed to the edge location that provides the lowest latency (time delay), so that content is delivered with the best possible performance.

What is the difference between S3 and CloudFront?

Amazon CloudFront works with S3 but copies files from S3 to the outer "edge" of Amazon's servers, allowing for fast retrieval. My tests show that it retrieves files in about half the time of S3. There's a slight increase in price from Amazon S3, but not much.


2 Answers

You can do this with lambdas here's the relevant lambda snippet.

exports.handler = (event, context, callback) => {
    // this is the request we want to re-map
    var request = event.Records[0].cf.request;

    // the request has a 'uri' property which is the value we want to overwrite
    // rewrite the url applying your custom logic
    request.uri = 'some custom logic here to rewrite the url';    

    // quit the lambda and let the request chain continue
    callback( null, request );    
};
like image 171
xenoterracide Avatar answered Oct 01 '22 01:10

xenoterracide


I don't think this is possible. An Origin Path can redirect to a subdirectory, (eg index.htm -> production/index.htm) but CloudFront can't strip out path portions.

See documentation: Origin Path

Some options:

  • Use a separate distribution instead of prefixing paths with s3
  • Just put s3/ at the front of your filenames (and actually use the full path)
like image 21
John Rotenstein Avatar answered Oct 01 '22 01:10

John Rotenstein