edit-icon download-icon

Invoke example

Last Updated: Apr 16, 2018

The Media Processing APIs are called by sending a request (an HTTP or HTTPS request) to the Media Processing server and receiving the server’s response to this request. Upon receiving the user request, the Media Processing server verifies the identity and parameters for the request. After all verification procedures are passed, the Media Processing server submits and completes relevant operations based on the specified parameter in the request, and returns the processing result to the caller through an HTTP response.

Procedure

  • Prepare request parameters.
  • Construct the Canonicalized Query String using the request parameters.
  • Construct the signature string.
  • Calculate the signature (including the HMAC signature and Base64 code).
  • Construct the request URL.

The following uses Java to show how to construct a request:

Global definition.

  1. private static final String ENCODE_TYPE = "UTF-8";
  2. private static final String ALGORITHM = "HmacSHA1";
  3. private static final String HTTP_METHOD = "GET";
  4. private static final String SEPARATOR = "&";
  5. private static final String EQUAL = "=";

Prepare request parameters.

  1. Map<String, String> parameterMap = new HashMap<String, String>();
  2. // Request public parameters.
  3. parameterMap.put("Action", "SearchTemplate");
  4. parameterMap.put("Version", "2014-06-18");
  5. parameterMap.put("AccessKeyId", "testId"); //Replace AccessKeyId with your AccessKeyId.
  6. parameterMap.put("Timestamp", "2015-05-14T09:03:45Z");//This timestamp is fixed for test. The signature value generated in the sample code remains unchanged. You can easily compare and verify the data. To generate a variable timestamp, replace this sentence with the following sentence:
  7. //parameterMap.put("Timestamp", formatIso8601Date(new Date()));
  8. parameterMap.put("SignatureMethod", "HMAC-SHA1");
  9. parameterMap.put("SignatureVersion", "1.0");
  10. parameterMap.put("SignatureNonce", "4902260a-516a-4b6a-a455-45b653cf6150"); //The unique random number is fixed for test. The signature value generated in the sample code remains unchanged. You can easily compare and verify the data. To generate a variable unique random number, replace this sentence with the following sentence:
  11. //parameterMap.put("SignatureNonce", UUID.randomUUID().toString());
  12. parameterMap.put("Format", "XML"); //The JSON format is also supported.

Construct the Canonicalized Query String using the request parameters.

  1. private static String percentEncode(String value) throws UnsupportedEncodingException {
  2. return URLEncoder.encode(value, ENCODE_TYPE).replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
  3. }
  4. private static String buildCanonicalizedQueryString(Map<String, String> parameterMap) throws UnsupportedEncodingException {
  5. // Sort the parameters.
  6. List<String> sortedKeys = new ArrayList<String>(parameterMap.keySet());
  7. Collections.sort(sortedKeys);
  8. StringBuilder temp = new StringBuilder();
  9. for (String key : sortedKeys) {
  10. // The key and value need to be encoded.
  11. String value = parameterMap.get(key);
  12. temp.append(SEPARATOR).append(percentEncode(key)).append(EQUAL).append(percentEncode(value));
  13. }
  14. return temp.toString().substring(1);
  15. }

Calculate the signature (including the HMAC signature and Base64 code).

  1. private static String buildStringToSign(String canonicalizedQueryString) throws UnsupportedEncodingException {
  2. // Generate the stringToSign characters.
  3. StringBuilder temp = new StringBuilder();
  4. temp.append(HTTP_METHOD).append(SEPARATOR);
  5. temp.append(percentEncode("/")).append(SEPARATOR);
  6. // The Canonicalized Query String needs to be encoded.
  7. temp.append(percentEncode(canonicalizedQueryString));
  8. return temp.toString();
  9. }
  10. private static String buildSignature(String keySecret, String stringToSign) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException {
  11. SecretKey key = new SecretKeySpec((keySecret + SEPARATOR).getBytes(ENCODE_TYPE), SignatureMethod.HMAC_SHA1);
  12. Mac mac = Mac.getInstance(ALGORITHM);
  13. mac.init(key);
  14. byte[] hashBytes = mac.doFinal(stringToSign.toString().getBytes(ENCODE_TYPE));
  15. byte[] base64Bytes = new Base64().encode(hashBytes);
  16. String base64UTF8String = new String(base64Bytes, "utf-8");
  17. return URLEncoder.encode(base64UTF8String, ENCODE_TYPE);
  18. }

Construct the request URL.

  1. private static String buildRequestURL(String signature, Map<String, String> parameterMap) throws UnsupportedEncodingException {
  2. // Generate the request URL.
  3. StringBuilder temp = new StringBuilder("http://mts.aliyuncs.com/?");
  4. temp.append(URLEncoder.encode("Signature", ENCODE_TYPE)).append("=").append(signature);
  5. for (Map.Entry<String, String> e : parameterMap.entrySet()) {
  6. temp.append("&").append(percentEncode(e.getKey())).append("=").append(percentEncode(e.getValue()));
  7. }
  8. return temp.toString();
  9. }

Examples

  • Prepare request parameters.

    • The value of Access Key ID istestId.
    • The value of Access Key Secret istestKeySecret.
    • The value of Timestamp is2015-05-14T09:03:45Z.
    • The value of SignatureNonce is4902260a-516a-4b6a-a455-45b653cf6150.
    • The value of Action isSearchTemplate, and the value of PageSize is2.
    • The value of Format isXML.
  • Construct the Canonicalized Query String using the request parameters.

    1. AccessKeyId=testId&Action=SearchTemplate&Format=XML&PageSize=2&SignatureMethod=HMAC-SHA1&SignatureNonce=4902260a-516a-4b6a-a455-45b653cf6150&SignatureVersion=1.0&Timestamp=2015-05-14T09%3A03%3A45Z&Version=2014-06-18
  • Construct the signature string.

    1. GET&%2F&AccessKeyId%3DtestId&Action%3DSearchTemplate&Format%3DXML&PageSize%3D2&SignatureMethod%3DHMAC-SHA1&SignatureNonce%3D4902260a-516a-4b6a-a455-45b653cf6150&SignatureVersion%3D1.0&Timestamp%3D2015-05-14T09%253A03%253A45Z&Version%3D2014-06-18
  • Calculate the signature (including the HMAC signature and Base64 code).

    1. kmDv4mWo806GWPjQMy2z4VhBBDQ%3D
  • Construct the request URL.

    1. http://mts.cn-hangzhou.aliyuncs.com/?Signature=kmDv4mWo806GWPjQMy2z4VhBBDQ%3D&SignatureVersion=1.0&Action=SearchTemplate&Format=XML&SignatureNonce=4902260a-516a-4b6a-a455-45b653cf6150&PageSize=2&Version=2014-06-18&AccessKeyId=testId&SignatureMethod=HMAC-SHA1&Timestamp=2015-05-14T09%3A03%3A45Z
  • Next, send an HTTP request (for example, curl) to the URL and obtain the response result (the response result in the sample code is in XML format) from the server.

    1. <SearchTemplateResponse>
    2. <RequestId>017F1B2D-2B5B-4441-ABBA-E0DC08F5AFEC</RequestId>
    3. <Template>
    4. <Id>88c6ca184c0e47098a5b665e2a126799</Id>
    5. <Name>MTS-example</Name>
    6. <Container>
    7. <Format>mp4</Format>
    8. </Container>
    9. <Video>
    10. <Codec>H.264</Codec>
    11. <Profile>high</Profile>
    12. <Bitrate>Auto</Bitrate>
    13. <Crf>15</Crf>
    14. <Width>256</Width>
    15. <Height>800</Height>
    16. <Fps>25</Fps>
    17. <Gop>10</Gop>
    18. <Preset>lower</Preset>
    19. <ScanMode></ScanMode>
    20. <Bufsize>6000</Bufsize>
    21. <Maxrate></Maxrate>
    22. <BitrateBnd>
    23. <Max></Max>
    24. <Min></Min>
    25. </BitrateBnd>
    26. </Video>
    27. <Audio>
    28. <Codec>aac</Codec>
    29. <Samplerate>44100</Samplerate>
    30. <Bitrate>500</Bitrate>
    31. <Channels>2</Channels>
    32. </Audio>
    33. <State>Normal</State>
    34. </Template>
    35. </SearchTemplateResponse>

Complete sample code

The sample code is written in Java, and the package manager is Maven.

Code download

JAR binary distribution download

Execution command: java -jar mts-signature-sample-all-0.1.jar testId testKeySecret 4902260a-516a-4b6a-a455-45b653cf6150 2015-05-14T09:03:45Z SearchTemplate XML PageSize@=2

Thank you! We've received your feedback.