Explicit Eager
Note: This module assumes you have strict transformations enabled.
Explicit Upload
Eager Transformations are a best practice in a multi-tenant system. You get better performance by having something already in place to be served. Explicit method is for already uploaded assets.
- Explicit method is for already uploaded assets.
- Eager transformations can be synchronous or asynchronous with a notification URL and we can issue invalidate to clear CDN cache.
Exercise: Create eager transformation
Start by uploading a new public image.
node access-control/explicit-eager/upload-public-asset.js
cloudinary.uploader.upload('./assets/images/killer-whale.jpg', {public_id: 'killer-whale',type: 'upload',overwrite: true,invalidate: true})
We won't be able to create on the fly transformations with this image because the Strict Transformations flag is enabled. Instead we'll use the explicit function of the Upload API SDK to "update" the asset with an eager transformation.
node access-control/explicit-eager/explicit-eager-upload-transform.js
Once this script is complete there will be a derived image ready for access on the CDN. We can open the secure URL for this derived image by accessing it from the response's eager array of eager transformations.
cloudinary.uploader.explicit('killer-whale',{type: 'upload',eager: [{width: 300,height: 300,quality: 'auto',crop: 'limit',invalidate: true}]}).then(result => {console.log('result', result)// look at the transformed urlconst transformUrl = result.eager[0].secure_urlconsole.log('transform url:', transformUrl)open(transformUrl)})
The above transformation was applied to a public asset. What if you want to use the explicit/eager transformation with an authenticated asset? Change the public id from killer-whale to dolphin which we uploaded earlier as authenticated. Also change the type to authenticated
. Does the explicit eager transformation still work?
cloudinary.uploader.explicit('dolphin',{type: 'authenticated',eager: [{width: 300,height: 300,quality: 'auto',crop: 'limit',invalidate: true}]})
It works and a signature is automatically added by the URL helper.
Strict Transformations Disabled
Don't forget to disable strict transformations as you continue through to the next section!