請使用下圖來了解當用戶端使用自訂應用程式處理數位資產時的端對端工作流程。
圖:使用處理資產的相關步 Asset Compute Service驟。
客戶端必須在向/process
發出第一個請求之前調用/register
一次,才能設定並檢索用於接收Adobe I/O事件以進行AdobeAsset compute的日記帳URL。
curl -X POST \
https://asset-compute.adobe.io/register \
-H "x-ims-org-id: $ORG_ID" \
-H "x-gw-ims-org-id: $ORG_ID" \
-H "Authorization: Bearer $JWT_TOKEN" \
-H "x-api-key: $API_KEY"
@adobe/asset-compute-client
JavaScript程式庫可用於NodeJS應用程式,以處理從註冊、處理到非同步事件處理的所有必要步驟。 有關所需標頭的詳細資訊,請參閱Authentication and Authorization。
客戶端發送processing請求。
curl -X POST \
https://asset-compute.adobe.io/process \
-H "x-ims-org-id: $ORG_ID" \
-H "x-gw-ims-org-id: $ORG_ID" \
-H "Authorization: Bearer $JWT_TOKEN" \
-H "x-api-key: $API_KEY" \
-d "<RENDITION_JSON>
用戶端負責使用預先簽署的URL正確格式化轉譯。 @adobe/node-cloud-blobstore-wrapper
JavaScript程式庫可用於NodeJS應用程式中,以預先簽署URL。 目前,程式庫僅支援Azure Blob儲存和AWS S3容器。
處理請求會傳回requestId
,可用於輪詢Adobe I/O事件。
以下是自訂應用程式處理請求的範例。
{
"source": "https://www.adobe.com/some-source-file.jpg",
"renditions" : [
{
"worker": "https://my-project-namespace.adobeioruntime.net/api/v1/web/my-namespace-version/my-worker",
"name": "rendition1.jpg",
"target": "https://some-presigned-put-url-for-rendition1.jpg",
}
],
"userData": {
"my-asset-id": "1234567890"
}
}
Asset Compute Service會將自訂應用程式轉譯請求傳送至自訂應用程式。 它會對提供的應用程式URL使用HTTPPOST,這是來自Project Firefly的安全Web動作URL。 所有要求都使用HTTPS通訊協定,以最大化資料安全性。
自訂應用程式使用的Asset computeSDK會處理HTTPPOST要求。 此外,它還可處理源的下載、上傳轉譯、傳送Adobe I/O事件和錯誤處理。
自訂程式碼只需提供回呼,並取用本機可用的原始碼檔案(source.path
)。 rendition.path
是放置資產處理請求最終結果的位置。 自訂應用程式使用回呼,使用傳入的名稱(rendition.path
),將本機可用的來源檔案轉換為轉譯檔案。 自定義應用程式必須寫入rendition.path
以建立格式副本:
const { worker } = require('@adobe/asset-compute-sdk');
const fs = require('fs').promises;
// worker() is the entry point in the SDK "framework".
// The asynchronous function defined is the rendition callback.
exports.main = worker(async (source, rendition) => {
// Tip: custom worker parameters are available in rendition.instructions.
console.log(rendition.instructions.name); // should print out `rendition.jpg`.
// Simplest example: copy the source file to the rendition file destination so as to transfer the asset as is without processing.
await fs.copyFile(source.path, rendition.path);
});
自訂應用程式只處理本機檔案。 下載源檔案由Asset computeSDK處理。
SDK會針對每個轉譯呼叫非同步轉譯回呼函式。
回呼函式可存取source和rendition物件。 source.path
已存在,是源檔案的本地副本的路徑。 rendition.path
是必須儲存已處理轉譯的路徑。 除非設定disableSourceDownload標幟,否則應用程式必須完全使用rendition.path
。 否則,SDK無法找到或識別轉譯檔案,且會失敗。
本示例的過度簡化旨在說明並重點介紹自定義應用程式的結構。 應用程式只會將源檔案複製到格式副本目的地。
如需轉譯回呼參數的詳細資訊,請參閱Asset computeSDK API。
建立每個轉譯並將其儲存在具有rendition.path
提供的路徑的檔案中後,Asset computeSDK會將每個轉譯上傳至雲端儲存空間(AWS或Azure)。 如果且唯有當傳入的請求有多個轉譯指向相同應用程式URL時,自訂應用程式才會同時取得多個轉譯。 上傳至雲端儲存空間會在每次轉譯後,以及在下次轉譯時執行回呼前完成。
batchWorker()
有不同的行為,因為它實際上會處理所有轉譯,而且只有在所有轉譯都已處理完畢後才會上傳這些轉譯。
SDK會針對每個轉譯傳送Adobe I/O事件。 視結果而定,這些事件為rendition_created
或rendition_failed
類型。 如需事件詳細資訊,請參閱Asset compute非同步事件 。
客戶端根據其消耗邏輯輪詢Adobe I/O 事件日誌。 初始日記帳URL是/register
API響應中提供的日誌URL。 可以使用requestId
來識別事件,該存在於事件中,且與/process
中傳回的相同。 每個轉譯都有個別的事件,會在轉譯上傳(或失敗)後立即傳送。 一旦收到相符的事件,用戶端就可以顯示或處理產生的轉譯。
JavaScript程式庫asset-compute-client
使用waitActivation()
方法來取得所有事件,讓日誌輪詢變得簡單。
const events = await assetCompute.waitActivation(requestId);
await Promise.all(events.map(event => {
if (event.type === "rendition_created") {
// get rendition from cloud storage location
}
else if (event.type === "rendition_failed") {
// failed to process
}
else {
// other event types
// (could be added in the future)
}
}));
有關如何獲取日誌事件的詳細資訊,請參閱Adobe I/O 事件API。