自定义Asset compute工作程序可以生成XMP(XML)数据,这些数据会发送回AEM,并作为元数据存储在资产上。
常见用例包括:
在本教程中,我们将创建一个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和 颜色名称)中使用的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代码(或您喜爱的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
基于文本的呈现。
在 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上获取,网址为: