Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate the external url for images in Sails?

Currently, Sails serves the images hosted in assets/images folder, however, I need a different approach because the images will be hosted in a CDN system (AWS Cloudfront), so the images' URLs follow a structure like this:

https://hdfhhfh.cloudtfront.net/images/image.jpg

Therefore, How can I approach this?, How can I approach this in order to Sails automatically generate the URL for me?

For dev env, it's ok, for production env I need all the images in HTML/EJS to point to the CDN.

like image 589
Ele Avatar asked Oct 16 '22 04:10

Ele


1 Answers

What you can do after uploading the images to S3 is:

  1. In CloudFront add an alias (Alternate Domain Names) for the default https://hdfhhfh.cloudtfront.net/ like https://images.example.com/
  2. In your Sails config (e.g. custom.js) add a global var like:

    images_url: 'https://images.example.com/'

then return this URL concatenated with the images/image.jpg e.g ${images_url}images/image.jpg from your API to your frontend.

like image 86
Robertino Vasilescu Avatar answered Oct 18 '22 23:10

Robertino Vasilescu