Snapshot jobs

Updated at:

By reading this article, you can learn the sample code for calling the OpenAPI to initiate and query the screenshot task of Smart Media Service through the server SDK.

Usage notes

Before you submit a snapshot job, you must create a snapshot template. Specify the TemplateId (snapshot template ID) when you submit the job. You can overwrite and reset the template parameters at submission time. After the job is processed, use the returned JobId (job ID) to query job details, list snapshot jobs, and obtain the access URLs for the snapshots.

Sample code

To debug online, use the Alibaba Cloud OpenAPI Developer Portal.

<?php

require_once '../vendor/autoload.php';

use AlibabaCloud\SDK\ICE\V20201109\ICE;
use AlibabaCloud\SDK\ICE\V20201109\Models;
use AlibabaCloud\SDK\ICE\V20201109\Models\ListSnapshotJobsRequest;
use AlibabaCloud\SDK\ICE\V20201109\Models\GetSnapshotUrlsRequest;
use AlibabaCloud\SDK\ICE\V20201109\Models\GetSnapshotJobRequest;
use AlibabaCloud\SDK\ICE\V20201109\Models\CreateCustomTemplateRequest;
use AlibabaCloud\SDK\ICE\V20201109\Models\SubmitSnapshotJobRequest;
use AlibabaCloud\SDK\ICE\V20201109\Models\SubmitSnapshotJobRequest\output;
use AlibabaCloud\SDK\ICE\V20201109\Models\SubmitSnapshotJobRequest\input;
use AlibabaCloud\SDK\ICE\V20201109\Models\SubmitSnapshotJobRequest\templateConfig;
use Darabonba\OpenApi\Models\Config;

try {

    // An AccessKey of an Alibaba Cloud account has permissions to access all APIs. Use a Resource Access Management (RAM) user for API calls and routine O&M.
    // This example shows how to store the AccessKey ID and AccessKey secret in environment variables. For more information about the configuration, see: https://help.aliyun.com/document_detail/311677.html
    $credential = new Credential([]);
    $config = new Config([
     'credential' => $credential,
     'endpoint' => 'ice.cn-shanghai.aliyuncs.com'
    ]);
    // If you want to hard-code the AccessKey ID and AccessKey secret, use the following code. However, we strongly recommend that you do not hard-code the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, which compromises the security of all resources in your account.
    // $config->accessKeyId = '<yourAccessKeyId>';
    // $config->accessKeySecret = '<yourAccessKeySecret>';
    $client = new ICE($config);

    // Create a snapshot template
    $snapshotTemplateConfig = "{\"Type\":\"Normal\",\"FrameType\":\"normal\",\"Time\":0,\"Count\":10}";
    $request = new CreateCustomTemplateRequest();
    $request->name = "sdk_sample_snapshot_template";
    $request->type = 2;
    $request->templateConfig = $snapshotTemplateConfig;
    $response = $client->createCustomTemplate($request);
    echo json_encode($response->body);

    // Submit a snapshot job
    $request = new SubmitSnapshotJobRequest();
    $request->name = "sdk_sample_snapshot_job";
    $request->input = new input(["type" => "Media","media" => "3945e42d01823498****"]);
    $request->output = new output(["type" => "Media","media" => "3945e42d01823498****"]);
    $request->templateConfig = new templateConfig(["templateId" => "4945e09631823498****"]);
    $response = $client->submitSnapshotJob($request);
    echo json_encode($response->body);

    // Query the details of a snapshot job
    $request = new GetSnapshotJobRequest();
    $request->jobId = "20b48fb04483915d4f2cd8ac****";
    $response = $client->getSnapshotJob($request);
    echo json_encode($response->body);

    // Obtain the list of snapshot jobs
    $request = new ListSnapshotJobsRequest();
    $request->pageSize = 5;
    $response = $client->listSnapshotJobs($request);
    echo json_encode($response->body);

    // Obtain the access URLs of the output images for a snapshot job
    $request = new GetSnapshotUrlsRequest();
    $request->pageSize = 5;
    $request->jobId = "20b48fb04483915d4f2cd8ac****";
    $response = $client->getSnapshotUrls($request);
    echo json_encode($response->body);

} catch (TeaUnableRetryError $e) {
    var_dump($e->getMessage());
}

Related API operations