开发Asset compute元数据工作程序
自定义Asset compute工作进程可以生成发送回AEM并作为元数据存储在资源上的XMP (XML)数据。
常见的用例包括:
- 与第三方系统(如PIM (产品信息管理系统))的集成,在这些第三方系统中,必须检索其他元数据并将其存储在资产上
- 与Adobe服务(如内容和Commerce AI)集成,使用其他机器学习属性来增强资源元数据
- 从资产的二进制文件获取有关资产的元数据,并将其存储为AEM as a Cloud Service中的资产元数据
您将要做什么
在本教程中,我们将创建一个Asset compute元数据工作器,该工作器可派生图像资源中最常用的颜色,并将这些颜色的名称写回到AEM中的资源元数据。 虽然工作进程本身是基础性的,但本教程将使用该工作进程来探索如何使用Asset compute工作进程将元数据写回AEM as a Cloud Service中的资源。
asset compute元数据工作进程调用的逻辑流
asset compute元数据工作程序的调用与生成工作程序的二进制演绎版的调用几乎相同,主要区别在于返回类型是XMP (XML)演绎版,其值也写入资源的元数据。
asset compute工作进程在renditionCallback(...)
函数中实施Asset computeSDK工作进程API合同,从概念上讲:
- 输入: AEM资产的原始二进制文件和处理配置文件参数
- 输出: XMP (XML)演绎版作为演绎版保留在AEM资源和资源的元数据中
- AEM Author服务调用Asset compute元数据工作程序,提供资产的 (1a) 原始二进制文件以及 (1b) 处理配置文件中定义的任何参数。
- asset computeSDK根据资产的二进制 (1a) 和任何处理配置文件参数 (1b),协调自定义Asset compute元数据工作进程的
renditionCallback(...)
函数的执行,从而生成XMP (XML)演绎版。 - asset compute工作进程将XMP (XML)表示形式保存到
rendition.path
。 - 写入到
rendition.path
的XMP (XML)数据将通过Asset computeSDK传输到AEM Author Service,并将它公开为 (4a) 文本演绎版,并且 (4b) 保留到资产的元数据节点。
配置manifest.yml manifest
必须在manifest.yml中注册所有Asset compute工作程序。
打开项目的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
将被删除以使用默认值。
开发元数据工作程序 metadata-worker
在Asset compute项目中创建新的元数据工作程序JavaScript文件,路径为为新的工作程序定义了manifest.yml,路径为/actions/metadata-colors/index.js
安装npm模块
安装此Asset compute工作进程中使用的额外npm模块(@adobe/asset-compute-xmp、get-image-colors和color-namer)。
$ 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; }
}
在本地运行元数据工作程序 development-tool
在工作程序代码完成后,可以使用本地Asset compute开发工具执行该代码。
由于我们的Asset compute项目包含两个工作程序(之前的circle演绎版和此metadata-colors
工作程序),Asset compute开发工具的配置文件定义将列出这两个工作程序的执行配置文件。 第二个配置文件定义指向新的metadata-colors
工作程序。
-
从Asset compute项目的根目录
-
执行
aio app run
以启动Asset compute开发工具 -
在 选择文件…… 下拉列表中,选取要处理的示例图像
-
在指向
metadata-colors
辅助进程的第二个配置文件定义配置中,当此辅助进程生成XMP (XML)演绎版时更新"name": "rendition.xml"
。 (可选)添加colorsFamily
参数(支持的值basic
、hex
、html
、ntc
、pantone
、roygbiv
)。code language-json { "renditions": [ { "worker": "...", "name": "rendition.xml", "colorsFamily": "pantone" } ] }
-
点按 运行 并等待生成XML呈现版本
- 由于这两个工作人员都在用户档案定义中列出,因此将生成这两个演绎版。 或者,可以删除指向circle演绎版工作进程的顶部配置文件定义,以避免从开发工具中执行它。
-
呈现版本 部分预览生成的呈现版本。 点按
rendition.xml
以下载它,然后在VS代码(或您喜爱的XML/文本编辑器)中打开它以进行查看。
测试工作程序 test
可以使用与二进制格式副本🔗相同的Asset compute测试框架来测试元数据工作程序。 唯一的区别是测试用例中的rendition.xxx
文件必须是预期的XMP (XML)演绎版。
-
在Asset compute项目中创建以下结构:
code language-none /test/asset-compute/metadata-colors/success-pantone/ file.jpg params.json rendition.xml
-
使用示例文件作为测试用例的
file.jpg
。 -
将以下JSON添加到
params.json
。code language-none { "fmt": "xml", "colorsFamily": "pantone" }
请注意,需要
"fmt": "xml"
才能指示测试包生成基于.xml
文本的演绎版。 -
在
rendition.xml
文件中提供所需的XML。 这可以通过以下方式获得:- 通过开发工具运行测试输入文件并保存(已验证的)XML呈现版本。
code language-none <?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>
-
从Asset compute项目的根执行
aio app test
以执行所有测试包。
将工作人员部署到Adobe I/O Runtime deploy
要从AEM Assets调用此新元数据工作程序,必须使用命令将其部署到Adobe I/O Runtime:
$ aio app deploy
请注意,这将部署项目中的所有工作人员。 查看未删节的部署说明,了解如何部署到暂存和生产工作区。
与AEM处理用户档案集成 processing-profile
通过创建新的自定义处理配置文件服务或修改现有的自定义处理配置文件服务,从AEM调用工作程序,此服务将调用已部署的工作程序。
-
以 AEM管理员 身份登录AEM as a Cloud Service创作服务
-
导航到 工具> Assets >处理配置文件
-
创建 新的处理配置文件,或 编辑 现有的处理配置文件
-
点按 自定义 选项卡,然后点按 新增
-
定义新服务
-
创建元数据演绎版:切换到活动状态
-
终结点:
https://...adobeioruntime.net/api/v1/web/wkndAemAssetCompute-0.0.1/metadata-colors
- 这是在部署期间或使用命令
aio app get-url
获得的辅助进程的URL。 根据AEM as a Cloud Service环境,确保URL指向正确的工作区。
- 这是在部署期间或使用命令
-
服务参数
-
点按 添加参数
- 键:
colorFamily
- 值:
pantone
- 支持的值:
basic
、hex
、html
、ntc
、pantone
、roygbiv
- 支持的值:
- 键:
-
-
Mime类型
- 包括:
image/jpeg
,image/png
,image/gif
,image/svg
- 这些是第三方npm模块支持的唯一用于派生颜色的MIME类型。
- 排除:
Leave blank
- 包括:
-
-
点按右上方的 保存
-
将处理配置文件应用到AEM Assets文件夹(如果尚未这样做)
更新元数据架构 metadata-schema
要查看颜色元数据,请将图像元数据架构上的两个新字段映射到工作进程填充的新元数据数据属性。
-
在AEM创作服务中,导航到 工具> Assets >元数据架构
-
导航到 默认 并选择并编辑 图像 并添加只读表单字段以公开生成的颜色元数据
-
添加 单行文本
- 字段标签:
Colors Family
- 映射到属性:
./jcr:content/metadata/wknd:colorsFamily
- 规则>字段>禁用编辑:已选中
- 字段标签:
-
添加 多值文本
- 字段标签:
Colors
- 映射到属性:
./jcr:content/metadata/wknd:colors
- 字段标签:
-
点按右上方的 保存
正在处理资产
在将颜色元数据写入资产的元数据后,在[dam:Asset]/jcr:content/metadata
资源上,此元数据被编入索引,提高了通过搜索使用这些术语发现资产的能力,甚至可以将其写回资产的二进制文件(如果在其上调用 DAM元数据写回 工作流)。
AEM Assets中的元数据演绎版
asset compute元数据工作进程生成的实际XMP文件也作为离散格式副本存储在资源上。 通常不使用此文件,而是使用对资源的元数据节点应用的值,但工作进程的原始XML输出在AEM中可用。
Github上的metadata-colors工作代码
Github上的最终metadata-colors/index.js
位于:
Github上的最终test/asset-compute/metadata-colors
测试套件位于: