自定义Asset compute工作进程可以生成发送回AEM的XMP (XML)数据,并将其作为元数据存储在资源上。
常见用例包括:
在本教程中,我们将创建一个Asset compute元数据工作程序,该程序可派生图像资源中最常用的颜色,并将这些颜色的名称写回资源的AEM元数据。 虽然工作器本身是基础函数,但本教程将使用该函数来探索如何使用Asset compute工作器将元数据写回AEMas a Cloud Service中的资源。
对Asset compute元数据工作程序的调用与的调用几乎相同 二进制演绎版生成工作程序,主要区别在于返回类型是XMP (XML)演绎版,其值也将写入资源的元数据。
asset compute工作程序可在中实施Asset computeSDK工作程序API合同 renditionCallback(...)
函数,其概念为:
renditionCallback(...)
函数,基于资产的二进制文件派生XMP (XML)演绎版 (1a) 和任何处理配置文件参数 (1b).rendition.path
.rendition.path
通过Asset computeSDK传输到AEM创作服务,并将其公开为 (4a) 文本呈现版本和 (4b) 保留到资源的元数据节点。所有Asset compute工作程序都必须在 manifest.yml.
打开项目的 manifest.yml
并添加用于配置新工作进程的工作进程条目,在本例中 metadata-colors
.
记住 .yml
区分空格。
packages:
__APP_PACKAGE__:
license: Apache-2.0
actions:
worker:
function: actions/worker/index.js
web: 'yes'
runtime: 'nodejs:12'
limits:
timeout: 60000 # in ms
memorySize: 512 # in MB
concurrency: 10
annotations:
require-adobe-auth: true
metadata-colors:
function: actions/metadata-colors/index.js
web: 'yes'
runtime: 'nodejs:12'
limits:
memorySize: 512 # in MB
function
指向在中创建的工作程序实施 下一步. 在语义上命名工作人员(例如, actions/worker/index.js
也许更适合指名 actions/rendition-circle/index.js
),如下所示 工作人员的URL 并确定 工作程序的测试包文件夹名称.
此 limits
和 require-adobe-auth
是按每个工作人员离散配置的。 在这个工人中, 512 MB
当代码检查(潜在)大的二进制图像数据时,分配存储器的容量。 另一个 limits
删除以使用默认值。
在路径下的Asset compute项目中创建新的元数据工作器JavaScript文件 为新工作进程定义了manifest.yml,位于 /actions/metadata-colors/index.js
安装额外的npm模块(@adobe/asset-compute-xmp, get-image-colors、和 color-name),该Asset compute工作进程中使用了该参数。
$ npm install @adobe/asset-compute-xmp
$ npm install get-image-colors
$ npm install color-namer
此工作人员看起来与 节目生成工作程序的主要区别是,它将XMP (XML)数据写入 rendition.path
以保存回AEM。
"use strict";
const { worker, SourceCorruptError } = require("@adobe/asset-compute-sdk");
const fs = require("fs").promises;
// Require the @adobe/asset-compute-xmp module to create XMP
const { serializeXmp } = require("@adobe/asset-compute-xmp");
// Require supporting npm modules to derive image colors from image data
const getColors = require("get-image-colors");
// Require supporting npm modules to convert image colors to color names
const namer = require("color-namer");
exports.main = worker(async (source, rendition, params) => {
// Perform any necessary source (input) checks
const stats = await fs.stat(source.path);
if (stats.size === 0) {
// Throw appropriate errors whenever an erring condition is met
throw new SourceCorruptError("source file is empty");
}
const MAX_COLORS = 10;
const DEFAULT_COLORS_FAMILY = 'basic';
// Read the color family parameter to use to derive the color names
let colorsFamily = rendition.instructions.colorsFamily || DEFAULT_COLORS_FAMILY;
if (['basic', 'hex', 'html', 'ntc', 'pantone', 'roygbiv'].indexOf(colorsFamily) === -1) {
colorsFamily = DEFAULT_COLORS_FAMILY;
}
// Use the `get-image-colors` module to derive the most common colors from the image
let colors = await getColors(source.path, { options: MAX_COLORS });
// Convert the color Chroma objects to their closest names
let colorNames = colors.map((color) => getColorName(colorsFamily, color));
// Serialize the data to XMP metadata
// These properties are written to the [dam:Asset]/jcr:content/metadata resource
// This stores
// - The list of color names is stored in a JCR property named `wknd:colors`
// - The colors family used to derive the color names is stored in a JCR property named `wknd:colorsFamily`
const xmp = serializeXmp({
// Use a Set to de-duplicate color names
"wknd:colors": [...new Set(colorNames)],
"wknd:colorsFamily": colorsFamily
}, {
// Define any property namespaces used in the above property/value definition
// These namespaces are automatically registered in AEM if they do not yet exist
namespaces: {
wknd: "https://wknd.site/assets/1.0/",
},
}
);
// Save the XMP metadata to be written back to the asset's metadata node
await fs.writeFile(rendition.path, xmp, "utf-8");
});
/**
* Helper function that derives the closest color name for the color, based on the colors family
*
* @param {*} colorsFamily the colors name family to use
* @param {*} color the color to convert to a name
*/
function getColorName(colorsFamily, color) {
if ('hex' === colorsFamily) { return color; }
let names = namer(color.rgb())[colorsFamily];
if (names.length >= 1) { return names[0].name; }
}
在工作程序代码完成后,可以使用本地Asset compute开发工具执行该代码。
因为我们的Asset compute项目包含两个工作进程(前一个 循环演绎版 以及这个 metadata-colors
worker), asset compute开发工具的 配置文件定义列出了两个工作人员的执行配置文件。 第二个配置文件定义指向新的 metadata-colors
工作人员。
从Asset compute项目的根目录
执行 aio app run
启动Asset compute开发工具
在 选择文件…… 下拉列表,选择 示例图像 待处理
在第二个用户档案定义配置中,它指向 metadata-colors
工作人员,更新 "name": "rendition.xml"
在此工作线程生成XMP (XML)演绎版时。 (可选)添加 colorsFamily
参数(支持的值) basic
, hex
, html
, ntc
, pantone
, roygbiv
)。
{
"renditions": [
{
"worker": "...",
"name": "rendition.xml",
"colorsFamily": "pantone"
}
]
}
点按 运行 并等待XML演绎版生成
此 演绎版 部分预览生成的解释。 点按 rendition.xml
要下载它,请在VS Code(或您喜爱的XML/文本编辑器)中打开它以进行审查。
可以使用测试元数据工作程序 与二进制格式副本相同的Asset compute测试框架. 唯一的区别是 rendition.xxx
测试用例中的文件必须是预期的XMP (XML)演绎版。
在Asset compute项目中创建以下结构:
/test/asset-compute/metadata-colors/success-pantone/
file.jpg
params.json
rendition.xml
使用 示例文件 作为测试用例的 file.jpg
.
将以下JSON添加到 params.json
.
{
"fmt": "xml",
"colorsFamily": "pantone"
}
请注意 "fmt": "xml"
指示测试包生成 .xml
基于文本的演绎版。
在中提供所需的XML rendition.xml
文件。 这可以通过以下方式获得:
<?xml version="1.0" encoding="UTF-8"?><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:wknd="https://wknd.site/assets/1.0/"><rdf:Description><wknd:colors><rdf:Seq><rdf:li>Silver</rdf:li><rdf:li>Black</rdf:li><rdf:li>Outer Space</rdf:li></rdf:Seq></wknd:colors><wknd:colorsFamily>pantone</wknd:colorsFamily></rdf:Description></rdf:RDF>
执行 aio app test
从Asset compute项目的根目录执行所有测试包。
要从AEM Assets调用此新元数据工作程序,必须使用命令将其部署到Adobe I/O Runtime:
$ aio app deploy
请注意,这将部署项目中的所有工作程序。 查看 未删节的部署指令 了解如何部署到暂存和生产工作区。
通过创建新或修改现有的自定义处理配置文件服务从AEM调用工作进程,该服务将调用此部署的工作进程。
https://...adobeioruntime.net/api/v1/web/wkndAemAssetCompute-0.0.1/metadata-colors
aio app get-url
. 根据AEMas a Cloud Service环境,确保URL指向正确的工作区。colorFamily
pantone
basic
, hex
, html
, ntc
, pantone
, roygbiv
image/jpeg
, image/png
, image/gif
, image/svg
Leave blank
要查看颜色元数据,请将图像元数据架构上的两个新字段映射到工作进程填充的新元数据数据属性。
Colors Family
./jcr:content/metadata/wknd:colorsFamily
Colors
./jcr:content/metadata/wknd:colors
Colors Family
和 Colors
元数据字段 对于从自定义Asset compute元数据工作器回写的元数据。将颜色元数据写入资产的元数据后,在 [dam:Asset]/jcr:content/metadata
对资源索引提高通过搜索使用这些术语的资源发现能力,如果超过该时间,甚至可以将它们写回资源的二进制文件 DAM元数据写回 在其上调用工作流。
asset compute元数据工作进程生成的实际XMP文件也作为离散格式副本存储在资源上。 通常不使用此文件,而是使用对资源的元数据节点应用的值,但工作进程的原始XML输出在AEM中可用。
决赛 metadata-colors/index.js
可在Github上获取,网址为:
决赛 test/asset-compute/metadata-colors
Github上的测试套件位于: