Creating an Encryption Task

You can create an encryption task by creating an MpcClient instance and setting related parameters.

Prerequisites

  • You have bought Object Storage Service (OBS) resources and uploaded an input file to an OBS bucket which is in the same region (for example, CN North-Beijing4) as MPC by referring to Uploading Media Files.
  • MPC has been authorized to access OBS resources. For details, see Authorizing Access to Cloud Resources.

Core Code

  1. Create MPC configuration items.
    These configuration items are used for MPC to obtain authorization. Table 1 describes the parameters.
    1
    2
    3
    4
    5
    MpcConfig mpcConfig = new MpcConfig();
    mpcConfig.setEndPoint(endPoint);// Set the endpoint.
    mpcConfig.setProjectId(projectId);// Set the project ID.
    mpcConfig.setSk(sk);// Set the SK.
    mpcConfig.setAk(ak);// Set the AK.
    
    Table 1 MPC parameters

    Parameter

    Type

    Description

    endPoint

    String

    Endpoint. For details, see Obtaining an Endpoint.

    ProjectId

    String

    Project ID. For details, see Obtaining a Project ID and Account Name.

    ak

    String

    Access key ID (AK). For details, see Obtaining the AK/SK Pair.

    sk

    String

    Secret Access Key (SK) used together with the AK. For details, see Obtaining the AK/SK Pair.

  2. Create an MpcClient instance.

    If no proxy server is configured, you can directly create an MpcClient instance.

    1
    2
    // Create an MpcClient instance.
     MpcClient mpcClient = new MpcClient(mpcConfig);
    

    If a proxy server needs to be configured, set proxy parameters and then transfer the proxy as a constructor to the MpcClient instance.

    1
    2
    3
    4
    5
    6
    7
    8
    // Configure the proxy.
    ClientConfig clientConfig = new ClientConfig();
           clientConfig.setProxyHost(proxyHost);// Set the IP address of the proxy server.
           clientConfig.setProxyPort(Integer.parseInt(proxyPort));// Set the port number of the proxy server.
           clientConfig.setProxyUserName(proxyUserName);// Set the username for accessing the proxy server.
           clientConfig.setProxyPassword(proxyPassword);// Set the password for accessing the proxy server.
    // Use the constructor to initialize MpcClient.
     MpcClient mpcClient =new MpcClient(mpcConfig, clientConfig);
    
  3. Create an encryption request.
    The request includes the input file, output file, and encryption parameter settings.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
         
            CreateEncryptRequest createEncryptRequest = new CreateEncryptRequest();
            ObsObjInfo input = new ObsObjInfo();
            // Set the OBS bucket for storing an input file.
            input.setBucket("obs-bills");
            // Set the region where the OBS bucket is located. You can view it on the OBS console.
            input.setLocation("cn-north-4");
            // Set the input file path.
            input.setObject("bills/index.m3u8");
            createEncryptRequest.setInput(input);
    
            ObsObjInfo output = new ObsObjInfo();
            // Set the OBS bucket for storing an input file.
            output.setBucket("obs-bills");
            // Set the region where the OBS bucket is located. You can view it on the OBS console.
            output.setLocation("cn-north-4");
            // Set the output file path.
            output.setObject("output/");
            createEncryptRequest.setOutput(output);
    
           // Set encryption parameters.
            CreateEncryptRequest.Encryption encryption = new CreateEncryptRequest.Encryption();
    
           /* CreateEncryptRequest.Encryption.Multidrm multidrm = new CreateEncryptRequest.Encryption.Multidrm();
           * // Set content_id.
           * multidrm.setContentId("contentId");
           * // Define the data stream type. The value can be DASH or HLS.
           * multidrm.setStreamingMode("streamingMode");
           * // Whether to enable audio encryption. The value 0 indicates that audio encryption is disabled and 1 indicates that audio encryption is enabled. The default value is 0.
           * multidrm.setEncryptAudio(1);
           * // Define the encryption method. The default value is 16420.
           * multidrm.setEmi(16420);
           * encryption.setMultidrm(multidrm);
            */
    
            CreateEncryptRequest.Encryption.HlsEncrypt hlsEncrypt = new CreateEncryptRequest.Encryption.HlsEncrypt();
    
            // Set encryption algorithms.
            hlsEncrypt.setAlgorithm("AES-128-CBC");
    
            // Set the key.
            hlsEncrypt.setKey("EpFDp4T1yyVA7YTB3QFNcw==");
    
            // Set the initialization vector.
            hlsEncrypt.setIv("nsADTFTiLVj7VCZ76HkhEw==");
    
            // Set the URL to obtain the key.
            hlsEncrypt.setUrl("http://10.154.193.244:16951/test/9b429fd8be7e47d89d6a6e1d34541136/d90e2dbacdb1619461d4012945c46175/asset/get_key");
            encryption.setHlsEncrypt(hlsEncrypt);
    
            createEncryptRequest.setEncryption(encryption);
            CreateEncryptResponse response = mpcClient.createEncryptTask(createEncryptRequest);
    

    Parameter

    Type

    Description

    bucketName

    String

    OBS bucket name

    location

    String

    Location of the input OBS bucket

    path

    String

    OBS object path, which complies with the OSS Object definition.

    If this parameter is used for an input, a specific object must be specified.

    If this parameter is used for an output, only the directory for storing the output needs to be specified.

  4. (Mandatory) Set encryption parameters.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
           // Set encryption parameters.
           CreateTranscodingRequest.Encryption encryption = new CreateTranscodingRequest.Encryption();
           CreateTranscodingRequest.Encryption.Multidrm multidrm = new CreateTranscodingRequest.Encryption.Multidrm();
           // Set content_id.
           multidrm.setContentId("contentId");
           // Define the data stream type. The value can be DASH or HLS.
           multidrm.setStreamingMode("HLS");
           // Whether to enable audio encryption. The value 0 indicates that audio encryption is disabled and 1 indicates that audio encryption is enabled. The default value is 0.
           multidrm.setEncryptAudio(1);
           // Define the encryption method. The default value is 16420.
           multidrm.setEmi(16420);
           multidrm.setDrmList(new String[]{"PLAYREADY"})
           encryption.setMultidrm(multidrm);
           createTranscodingRequest.setEncryption(encryption);
    

Full Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.huawei.mpc.client.MpcClient;
import com.huawei.mpc.client.MpcConfig;
import com.huawei.mpc.model.BaseResponse;
import com.huawei.mpc.model.ObsObjInfo;
import com.huawei.mpc.model.encrypt.CreateEncryptRequest;
import com.huawei.mpc.model.encrypt.CreateEncryptResponse;
import com.huawei.mpc.model.encrypt.QueryEncryptRequest;
import com.huawei.mpc.model.encrypt.QueryEncryptResponse;
importjava.util.ArrayList;
importjava.util.List;

        MpcConfig mpcConfig = new MpcConfig();

        // Authentication method 1: AK/SK authentication (default method). For details about how to obtain the AK/SK pair, see section "Obtaining Key Parameters."
        // mpcConfig.setAk("YS0JVWUSSQ23QL2PBMVH");
        // mpcConfig.setSk("HCxKJ2plAR8h5kmP3jOeaXo9sXAb1ROeOMcKOnkR");

        // Set the language. Chinese and English are supported. Chinese is the default language.
        // mpcConfig.setLanguage(MpcConfig.LanguageEnum.EN_US.getName());

        // Authentication method 2: obtain temporary authorization and then perform authentication.
        /*
         * mpcConfig.setAk(""); 
         * mpcConfig.setSk(""); 
         * mpcConfig.setSecurityToken("");
         */

        // Authentication method 3: authentication using the username and password (DomainName is the same as the username by default). For details, see section "Obtaining Key Parameters."
        mpcConfig.setUsername("VOD_wwx530051");
        mpcConfig.setPassword("Huawei@123");
        mpcConfig.setDomainName("VOD_wwx530051");
        mpcConfig.setIamEndPoint("192.144.1.37:31943");
        // mpcConfig.setIamEndPoint("192.145.63.209:31943");

        // Set the endpoint of MPC. For details, see section "Obtaining Key Parameters."
        mpcConfig.setEndPoint("mts.cn-north-4.myhuaweicloud.com");
        // Set the project ID. For details, see section "Obtaining Key Parameters."
        mpcConfig.setProjectId("14ce1d4437164aba8b364ce15866154e");
        mpcClient = new MpcClient(mpcConfig);

        CreateEncryptRequest createEncryptRequest = new CreateEncryptRequest();
        ObsObjInfo input = new ObsObjInfo();
        // Set the OBS bucket for storing an input file.
        input.setBucket("obs-bills");
        // Set the region where the OBS bucket is located. You can view it on the OBS console.
        input.setLocation("cn-north-4");
        // Set the input file path.
        input.setObject("bills/1080P_2.mp4");
        createEncryptRequest.setInput(input);

        ObsObjInfo output = new ObsObjInfo();
        // Set the OBS bucket for storing an input file.
        output.setBucket("obs-bills");
        // Set the region where the OBS bucket is located. You can view it on the OBS console.
        output.setLocation("cn-north-4");
        // Set the output file path.
        output.setObject("output/");
        createEncryptRequest.setOutput(output);

       // Set encryption parameters.
        CreateEncryptRequest.Encryption encryption = new CreateEncryptRequest.Encryption();

        /*
         * CreateEncryptRequest.Encryption.Multidrm multidrm = new CreateEncryptRequest.Encryption.Multidrm();
         * // Set content_id.
         *  multidrm.setContentId("contentId"); 
       * // Define the data stream type. The value can be DASH or HLS.
         * multidrm.setStreamingMode("streamingMode"); 
         * // Whether to enable audio encryption. The value 0 indicates that audio encryption is disabled and 1 indicates that audio encryption is enabled. The default value is 0.
         * multidrm.setEncryptAudio(1);
         * // Define the encryption method.
         *  multidrm.setEmi(16420); 
         *  encryption.setMultidrm(multidrm);
         */

        CreateEncryptRequest.Encryption.HlsEncrypt hlsEncrypt = new CreateEncryptRequest.Encryption.HlsEncrypt();

        // Set encryption algorithms.
        hlsEncrypt.setAlgorithm("AES-128-CBC");

        // Set the key.
        hlsEncrypt.setKey("EpFDp4T1yyVA7YTB3QFNcw==");

        // Set the initialization vector.
        hlsEncrypt.setIv("nsADTFTiLVj7VCZ76HkhEw==");

        // Set the URL to obtain the key.
        hlsEncrypt.setUrl("http://10.154.193.244:16951/test/9b429fd8be7e47d89d6a6e1d34541136/d90e2dbacdb1619461d4012945c46175/asset/get_key");
        encryption.setHlsEncrypt(hlsEncrypt);

        createEncryptRequest.setEncryption(encryption);
        CreateEncryptResponse response = mpcClient.createEncryptTask(createEncryptRequest);