cancel
Showing results for 
Search instead for 
Did you mean: 

How to implement cloudfront for s3media url strategy

Former Member

I have configured S3 media storage strategy for my contents. While initialize its generated data in s3. Now how can i implement cloudfront for s3 media url strategy? So that my contents will serve from cloudfront.

Accepted Solutions (1)

Accepted Solutions (1)

matthewsmith
Participant

Implementing a CloudFront URL strategy is fairly easy. Set up Hybris to use the S3 media storage as normal, and configure your CloudFront distribution to serve the bucket's content - either via signed or unsigned urls, according to your requirements.

Then, you'll need to write a new custom class - CloudFrontMediaUrlStrategy. If you don't require signed URLs, the code should be extremely simple:

String distributionDomain = config.getParameter("url.domain");
final String location = HttpUtils.urlEncode(media.getLocation(), true);
url = "https://" + distributionDomain + "/" + location;
return url;

You can then configure your bucket like so:

(...usual S3 media storage properties here...)
media.folder.myfolder.url.strategy=cloudFrontMediaURLStrategy
media.folder.myfolder.url.domain=mydistribution.cloudfront.net
media.folder.myfolder.url.signed=false

If you require signed URLs, it's a little more fiddly. Add a dependency to your custom extension to the AWS Java SDK - CloudFront component:

<dependency>
   <groupId>com.amazonaws</groupId>
   <artifactId>aws-java-sdk-cloudfront</artifactId>
   <version>${aws.version}</version>
</dependency>

(I used the same aws.version as the amazoncloud extension for my version of Hybris uses)

Then, you basically need to call CloudFrontUrlSigner.getSignedURLWithCannedPolicy() (or one of the other functions if you need a custom policy) to generate your URLs. Check out the API documentation for more information.

You'll need to add some extra parameters to your bucket configuration so that you've got the necessary information, and access those parameters from within your URL strategy class:

media.folder.myfolder.url.signed=true
media.folder.myfolder.url.signed.keypair=mycloudfrontkeypairid
media.folder.myfolder.url.signed.keyfile=/path/to/cloudfront/signer/private/key.pem
media.folder.myfolder.url.signed.validFor=30 #how long the url is valid for (in minutes)

Beware that there is a bug with the url.signed.validFor property before 6.7 - you'll need to implement a small fix in Spring config or use a different property name.

Answers (1)

Answers (1)

Former Member

There is no default solution to integrate CloudFront into a hybris platform environment.