Lesson 3: Implement the worker
Create a new application using AIO CLI
Copied to your clipboard$> aio app init my-custom-worker
Application initialization will ask you a couple of questions:
- To select your Adobe Organization, followed by the console project selection (pick the one you created in previous steps) and finally choose a project workspace where you added all the required services.
- Then, to pick the components of the app. Select only Actions: Deploy Runtime action.
- On the type of action, choose only: Adobe Asset Compute worker.
- At last step, you need to provide the name of the worker and wait for the
npm
to finish installing all the dependencies.
Once it's done, edit .env
file and add the following lines. These are the environment variables the AIO CLI uses. In a
production deployment, you can set them directly on your CI/CD pipelines as environment variables.
Copied to your clipboard## A path to the private.key you obtained from Adobe ConsoleASSET_COMPUTE_PRIVATE_KEY_FILE_PATH=/path/to/the/private.key## Azure blob storage container you created to simulate AEM binaries cloud storageAZURE_STORAGE_ACCOUNT=your-storage-accountAZURE_STORAGE_KEY=your-storage-keyAZURE_STORAGE_CONTAINER_NAME=source# Azure blob storage container used by the imgIX as assets sourceIMGIX_STORAGE_ACCOUNT=your-storage-accountIMGIX_STORAGE_KEY=your-storage-keyIMGIX_STORAGE_CONTAINER_NAME=imgix# A security token you obtained when setting up imgIX sourceIMGIX_SECURE_TOKEN=imgx-token# A imgix domain you defined when setting up imgIX sourceIMGIX_DOMAIN=your-subdomain.imgix.net
Edit ext.config.yaml
file inside the src/dx-asset-compute-worker-1/
folder and add inputs
object, as shown below. This file describes IO Runtime action to be deployed.
And input
param sets the default parameters with values referenced to our environment variables. Those params are
available in action JS as param
object.
Copied to your clipboardoperations:workerProcess:- type: actionimpl: dx-asset-compute-worker-1/workerhooks:post-app-run: adobe-asset-compute devtooltest: adobe-asset-compute test-workeractions: actionsruntimeManifest:packages:dx-asset-compute-worker-1:license: Apache-2.0actions:czeczek-worker:function: actions/worker/index.jsweb: 'yes'runtime: 'nodejs:14'limits:concurrency: 10inputs:imgixStorageAccount: $IMGIX_STORAGE_ACCOUNTimgixStorageKey: $IMGIX_STORAGE_KEYimgixStorageContainerName: $IMGIX_STORAGE_CONTAINER_NAMEimgixSecureToken: $IMGIX_SECURE_TOKENimgixDomain: $IMGIX_DOMAINannotations:require-adobe-auth: truefinal: true
We also need to add two dependencies to our project. These are the libraries we will use to simplify access to the Azure blob storage and to generated signed URL for imgIX.
Copied to your clipboard$> npm install @adobe/aio-lib-files imgix-core-js
Finally, edit the worker source code (located under src/dx-asset-compute-worker-1/actions/<worker-name>/index.js
) and replace it
with the following code.
Copied to your clipboard'use strict';const { worker } = require('@adobe/asset-compute-sdk');//Convinient library provided by adobe that abstract away managing files on cloud storagesconst filesLib = require('@adobe/aio-lib-files');const { downloadFile } = require('@adobe/httptransfer');const ImgixClient = require('imgix-core-js');exports.main = worker(async (source, rendition, params) => {//Initialize blob storage client used by imgix//We're reading the parameters we defined in manifest.ymlconst targetStorage = await filesLib.init({azure: {storageAccount: params.imgixStorageAccount,storageAccessKey: params.imgixStorageKey,containerName: params.imgixStorageContainerName,},});//Copy source asset from the AEM binaries storage to the Azure blob storage for imgIX// localSrc:true means, the first parameters points to the file in the local file system (asset-compute-sdk abstracts the source blob storage so it's visible as local file)// Second arguments defines the path on the target blob storage. We use the same path just to simplify thingsawait targetStorage.copy(source.path, source.path, { localSrc: true });//Initialize imgix client responsible for generation of signed URLs//to our assets accessed via imgIX subdomain//We're getting the config params we defined in manifest.ymlconst client = new ImgixClient({domain: params.imgixDomain,secureURLToken: params.imgixSecureToken,});//Generate signed URL with the params send by AEM and sign it.//All the parameters send by AEM are available under rendition.instructions objectconst url = client.buildURL(source.path, JSON.parse(rendition.instructions.imgix));//Finally, download a rendition from a given url and store in AEM azure blob storage so it will be visible in AEM as a renditionawait downloadFile(url, rendition.path);});