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

You can perform online debugging in the Alibaba Cloud OpenAPI Developer Portal.

package main

import (
    "fmt"
    openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
    ice "github.com/alibabacloud-go/ice-20201109/v2/client"
    "github.com/aliyun/credentials-go/credentials"
    "github.com/alibabacloud-go/tea/tea"
)

func main() {

    // An AccessKey of an Alibaba Cloud account has all API access permissions. Use a Resource Access Management (RAM) user to call API operations or perform routine O&M.
    // This example shows how to store the AccessKey ID and AccessKey secret in environment variables.
    credential, _err := credentials.NewCredential(nil)
    if _err != nil {
      panic(_err)
    }
    iceClient, _err := ice.NewClient(&openapi.Config{
      Credential: credential,
      Endpoint: tea.String("ice.cn-shanghai.aliyuncs.com"),
    })

    // To hard-code the AccessKey ID and AccessKey secret, use the following code. Do not hard-code the AccessKey ID and AccessKey secret in your project code. Hard coding may cause an AccessKey leak and threaten the security of all resources in your account.
    // config := new(openapi.Config)
    // config.SetAccessKeyId("<accessKeyId>").
    //     SetAccessKeySecret("<accessKeySecret>").
    //     SetRegionId("<regionId>").
    //     SetEndpoint("<endpoint>")
    // iceClient, _err := ice.NewClient(config)

    if _err != nil {
      panic(_err)
    }

    // Create a snapshot template.
    templateName := "sdk_sample_snapshot_template"
    templateType := int32(2)
    snapshotTemplateConfig := "{\"Type\":\"Normal\",\"FrameType\":\"normal\",\"Time\":0,\"Count\":10}"
    createCustomTemplateRequest := &ice.CreateCustomTemplateRequest{
        Name:           &templateName,
        Type:           &templateType,
        TemplateConfig: &snapshotTemplateConfig,
    }
    createCustomTemplateResponse, err := iceClient.CreateCustomTemplate(createCustomTemplateRequest)
    fmt.Println(createCustomTemplateResponse, err)

    // Submit a snapshot job.
    templateId := createCustomTemplateResponse.Body.CustomTemplate.TemplateId
    jobName := "sdk_sample_snapshot_job"
    inputType := "Media"
    inputMedia := "3945e42d-1823498****"
    outputType := "Media"
    outputMedia := "3945e42d-1823498****"
    submitSnapshotJobRequest := &ice.SubmitSnapshotJobRequest{
        Name: &jobName,
        Input: &ice.SubmitSnapshotJobRequestInput{
            Type:  &inputType,
            Media: &inputMedia,
        },
        Output: &ice.SubmitSnapshotJobRequestOutput{
            Type:  &outputType,
            Media: &outputMedia,
        },
        TemplateConfig: &ice.SubmitSnapshotJobRequestTemplateConfig{
            TemplateId: templateId,
        },
    }
    submitSnapshotJobResponse, err := iceClient.SubmitSnapshotJob(submitSnapshotJobRequest)
    fmt.Println(submitSnapshotJobResponse, err)

    // Query the details of the snapshot job.
    jobId := submitSnapshotJobResponse.Body.JobId
    getSnapshotJobRequest := &ice.GetSnapshotJobRequest{
        JobId: jobId,
    }
    getSnapshotJobResponse, err := iceClient.GetSnapshotJob(getSnapshotJobRequest)
    fmt.Println(getSnapshotJobResponse, err)

    // Obtain the list of snapshot jobs.
    pageSize := int32(5)
    listSnapshotJobsRequest := &ice.ListSnapshotJobsRequest{
        PageSize: &pageSize,
    }
    listSnapshotJobsResponse, err := iceClient.ListSnapshotJobs(listSnapshotJobsRequest)
    fmt.Println(listSnapshotJobsResponse, err)

    // Obtain the access URLs of the output images for the snapshot job.
    getSnapshotUrlsRequest := &ice.GetSnapshotUrlsRequest{
        JobId:    jobId,
        PageSize: &pageSize,
    }
    getSnapshotUrlsResponse, err := iceClient.GetSnapshotUrls(getSnapshotUrlsRequest)
    fmt.Println(getSnapshotUrlsResponse, err)
}      

Related API operations