@adobe/asset-compute-client 中文文档教程
Adobe Asset Compute Client
Overview
Adobe 资产计算服务的 Javascript 客户端。 目前仅使用 Nodejs 进行测试。 Javascript API 分为 3 个部分:
- AssetCompute - A light-weight wrapper around the AssetCompute API.
- AssetComputeEventEmitter - Listens to an I/O event journal and converts the events to
rendition_created
andrendition_failed
events. - AssetComputeClient - A higher level client that provides a simpler API
- by default, uses
node-fetch-retry
for retrying on other HTTP responses - by default, provides custom, smarter retry behavior on HTTP status code 429 (Too many requests).
- by default, uses
AssetComputeClient 具有以下功能:
- Fully initialize Asset Compute through a previously provisioned integration
- Listens to I/O events on the integration
- Invoke Asset Compute process asynchronously
- Wait for a single Asset Compute process request to finish (default timeout is 60s)
- Wait for all Asset Compute process requests to finish (default timeout is 60s)
retry
具有以下功能:
- additional features to retry on 429s for
/unregister
,/register
, and/process
- Looks at the
retry-after
header in the HTTP response to determine how long to wait (in seconds) before retrying - If no
retry-after
is present, choose a random wait time between 30-60 seconds - Configurable retry count via
max429RetryCount
option. (Defaults to 4 retries) - Disable completely via the
disable429Retry
option. (Defaults to 4 retries)
Installation
npm i @adobe/asset-compute-client
Usage
Using the Class Initialization
设置客户端后,您必须在第一次调用 .process()
之前调用一次 .register()
。
如果集成还没有注册 I/O 事件日志,调用 .register()
后可能需要一些时间才能接收和发送 I/O 事件,因此建议添加在调用 .process()
之前等待一些时间。
如果集成已经注册了 I/O 事件日志,建议在调用 .process()
之前不要等待。
const { AssetComputeClient, getIntegrationConfiguration } = require("@adobe/asset-compute-client");
const sleep = require('util').promisify(setTimeout);
//If integration file is json, a private key file must also be provided
const integrationFilePath = "/path/to/integration/file"; // Either json or yaml format
const integration = await getIntegrationConfiguration(integrationFilePath[, privateKeyFile]);
const assetCompute = new AssetComputeClient(integration);
// Call register before first call the process
await assetCompute.register();
// add wait time for events provider to set up
await sleep(45000); // 30s
const { requestId } = await assetCompute.process(
"https://presigned-source-url", [
{
name: "rendition.png",
url: "https://presigned-target-url",
fmt: "png",
width: 200,
height: 200
}
]
)
const events = await assetCompute.waitActivation(requestId);
if (events[0].type === "rendition_created") {
// use the rendition
} else {
// failed to process
}
Error message printing
请注意,轮询 I/O 事件日志时的任何错误都将在重试之前记录下来:
Error polling event journal: request to https://events-va6.adobe.io/.... failed, reason: connect ECONNREFUSED 54.81.231.29:443
要添加自定义错误消息处理,请侦听 error
事件:
assetCompute.on("error", error => console.log("custom error message", error.message));
或禁用任何错误消息输出:
assetCompute.on("error", () => {});
Using AssetComputeClient.create()
for Initialization
此函数创建一个AssetComputeClient
的新实例并调用 .register()
方法。
const { AssetComputeClient, getIntegrationConfiguration } = require("@adobe/asset-compute-client");
//If integration file is json, a private key file must also be provided
const integrationFilePath = "/path/to/integration/file"; // Either json or yaml format
const integration = await getIntegrationConfiguration(integrationFilePath[, privateKeyFile]);
const assetCompute = await AssetComputeClient.create(integration);
// add wait time if needed
const { requestId } = await assetCompute.process(
"https://presigned-source-url", [
{
name: "rendition.png",
url: "https://presigned-target-url",
fmt: "png",
width: 200,
height: 200
}
]
)
const events = await assetCompute.waitActivation(requestId);
if (events[0].type === "rendition_created") {
// use the rendition
} else {
// failed to process
}
Register
设置客户端后,在调用.process()
之前需要调用一次.register()
。
如果集成已经注册了 I/O 事件日志,您仍然必须调用注册。 从 register 返回的日志 url 是客户端检索 I/O 事件所必需的。
如果集成没有注册 I/O 事件日志,请确保在调用 .register()
之后再调用 .process()
之前添加一些等待时间。 (建议等待约 45 秒)
const assetCompute = new AssetComputeClient(integration);
await assetCompute.register();
Unregister
取消注册方法将删除在 .register()
中创建的 I/O 事件日志。 在注销后尝试使用客户端之前,需要再次调用 .register()
。
用法示例:
const assetCompute = new AssetComputeClient(integration);
await assetCompute.register();
await assetCompute.process(..renditions);
// unregister journal
await assetCompute.unregister();
// call to process will fail, must call `register()` again first
try {
await assetCompute.process(..renditions);
} catch (e) {
// expected error, must call `register()` first
}
await assetCompute.register();
sleep(45000); // sleep after registering to give time for journal to set up
await assetCompute.process(..renditions);
Using custom 429 retry options
默认情况下,AssetComputeClient
将在 429 时重试 4 次(使用智能背压)。
在 429 上重试 10 次:在 429 上
const assetCompute = new AssetComputeClient(integration, {
max429RetryCount: 10
});
禁用重试:
const assetCompute = new AssetComputeClient(integration, {
disable429Retry: false
});
@adobe/node-fetch-retry
获取重试选项记录在此处。 默认选项用于每个获取请求。
注意:这些不包括在 429s 上重试,因为这需要自定义重试逻辑 (retry.js)(即,在端点过载的情况下,每 1s 重试一次退避可能会使问题恶化)
Contributing
欢迎贡献! 阅读贡献指南了解更多信息。
Licensing
这个项目是根据 Apache V2 许可证获得许可的。 有关详细信息,请参阅许可证。
Adobe Asset Compute Client
Overview
Javascript client for the Adobe Asset Compute Service. Currently only tested with Nodejs. The Javascript API is separated in 3 parts:
- AssetCompute - A light-weight wrapper around the AssetCompute API.
- AssetComputeEventEmitter - Listens to an I/O event journal and converts the events to
rendition_created
andrendition_failed
events. - AssetComputeClient - A higher level client that provides a simpler API
- by default, uses
node-fetch-retry
for retrying on other HTTP responses - by default, provides custom, smarter retry behavior on HTTP status code 429 (Too many requests).
- by default, uses
AssetComputeClient has the following capabilities:
- Fully initialize Asset Compute through a previously provisioned integration
- Listens to I/O events on the integration
- Invoke Asset Compute process asynchronously
- Wait for a single Asset Compute process request to finish (default timeout is 60s)
- Wait for all Asset Compute process requests to finish (default timeout is 60s)
retry
has the following capabilities:
- additional features to retry on 429s for
/unregister
,/register
, and/process
- Looks at the
retry-after
header in the HTTP response to determine how long to wait (in seconds) before retrying - If no
retry-after
is present, choose a random wait time between 30-60 seconds - Configurable retry count via
max429RetryCount
option. (Defaults to 4 retries) - Disable completely via the
disable429Retry
option. (Defaults to 4 retries)
Installation
npm i @adobe/asset-compute-client
Usage
Using the Class Initialization
After the client is set up, you must call .register()
once before the first call to .process()
.
If the integration does not already have an I/O Events journal registered, it may take some time after calling .register()
to be able to recieve and send I/O Events so it is recommended to add some wait time before calling .process()
.
If the integration already has an I/O Events journal registered, it is recommended to not wait before calling .process()
.
const { AssetComputeClient, getIntegrationConfiguration } = require("@adobe/asset-compute-client");
const sleep = require('util').promisify(setTimeout);
//If integration file is json, a private key file must also be provided
const integrationFilePath = "/path/to/integration/file"; // Either json or yaml format
const integration = await getIntegrationConfiguration(integrationFilePath[, privateKeyFile]);
const assetCompute = new AssetComputeClient(integration);
// Call register before first call the process
await assetCompute.register();
// add wait time for events provider to set up
await sleep(45000); // 30s
const { requestId } = await assetCompute.process(
"https://presigned-source-url", [
{
name: "rendition.png",
url: "https://presigned-target-url",
fmt: "png",
width: 200,
height: 200
}
]
)
const events = await assetCompute.waitActivation(requestId);
if (events[0].type === "rendition_created") {
// use the rendition
} else {
// failed to process
}
Error message printing
Note that any errors while polling the I/O Event journal will be logged before it retries:
Error polling event journal: request to https://events-va6.adobe.io/.... failed, reason: connect ECONNREFUSED 54.81.231.29:443
To add custom error message handling, listen for the error
event:
assetCompute.on("error", error => console.log("custom error message", error.message));
Or disable any error message output:
assetCompute.on("error", () => {});
Using AssetComputeClient.create()
for Initialization
This function creates a new instance of AssetComputeClient
and calls the .register()
method.
const { AssetComputeClient, getIntegrationConfiguration } = require("@adobe/asset-compute-client");
//If integration file is json, a private key file must also be provided
const integrationFilePath = "/path/to/integration/file"; // Either json or yaml format
const integration = await getIntegrationConfiguration(integrationFilePath[, privateKeyFile]);
const assetCompute = await AssetComputeClient.create(integration);
// add wait time if needed
const { requestId } = await assetCompute.process(
"https://presigned-source-url", [
{
name: "rendition.png",
url: "https://presigned-target-url",
fmt: "png",
width: 200,
height: 200
}
]
)
const events = await assetCompute.waitActivation(requestId);
if (events[0].type === "rendition_created") {
// use the rendition
} else {
// failed to process
}
Register
After setting up the client, it is necessary to call .register()
once before calling .process()
.
If the integration already has an I/O Events journal registered, you still must call register. The journal url returned from register is necessary for the client to retrieve I/O Events.
If the integration does not have an I/O Events journal registered, make sure to add some wait time after calling .register()
before calling .process()
. (It is recommended to wait around ~45 seconds)
const assetCompute = new AssetComputeClient(integration);
await assetCompute.register();
Unregister
The unregister method will remove the I/O Events Journal created in .register()
. It is necessary to call .register()
again before attempting to use the client after unregistering.
Example usage:
const assetCompute = new AssetComputeClient(integration);
await assetCompute.register();
await assetCompute.process(..renditions);
// unregister journal
await assetCompute.unregister();
// call to process will fail, must call `register()` again first
try {
await assetCompute.process(..renditions);
} catch (e) {
// expected error, must call `register()` first
}
await assetCompute.register();
sleep(45000); // sleep after registering to give time for journal to set up
await assetCompute.process(..renditions);
Using custom 429 retry options
By default, AssetComputeClient
will retry 4 times (with smart backpressure) on 429s.
Retry 10 times on 429s:
const assetCompute = new AssetComputeClient(integration, {
max429RetryCount: 10
});
Disable retry on 429s:
const assetCompute = new AssetComputeClient(integration, {
disable429Retry: false
});
@adobe/node-fetch-retry
Fetch retry options are documented here. The default options are used on each fetch request.
Note: these do not cover retrying on 429s since this requires the custom retry logic (retry.js) (ie, retrying every 1s with backoff could worsen the issue in a situation when the endpoint is overloaded)
Contributing
Contributions are welcomed! Read the Contributing Guide for more information.
Licensing
This project is licensed under the Apache V2 License. See LICENSE for more information.