edit-icon download-icon

Query media - Use OSS file address

Last Updated: Jan 02, 2018

If you do not know the media ID (a live video converted to an on-demand video using the media workflow), you can use the media input URL to query the media information over QueryMediaListByURL.

  1. package com.aliyun.mts.api.demo
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.aliyuncs.DefaultAcsClient;
  4. import com.aliyuncs.exceptions.ClientException;
  5. import com.aliyuncs.mts.model.v20140618.QueryMediaListByURLRequest;
  6. import com.aliyuncs.mts.model.v20140618.QueryMediaListByURLResponse;
  7. import com.aliyuncs.profile.DefaultProfile;
  8. import java.io.UnsupportedEncodingException;
  9. import java.net.URLEncoder;
  10. public class QueryMediaListByURLDemo {
  11. private DefaultAcsClient client = null;
  12. private final String REGION = "<region>";
  13. private final String ID="<accessKeyId>";
  14. private final String KEY ="<accessKeySecret>";
  15. public QueryMediaListByURLDemo() throws ClientException {
  16. this.client = new DefaultAcsClient(DefaultProfile.getProfile(REGION, ID, KEY));
  17. }
  18. //Query the media information, such as the media ID, media status, and other attributes, based on the OSS URL of the media source
  19. private void queryMediaListByURL() throws ClientException, UnsupportedEncodingException {
  20. QueryMediaListByURLRequest request = new QueryMediaListByURLRequest();
  21. String ossHost = 'http://<input-bucket>.<region>.aliyuncs.com/';
  22. String ossObject = "test/The Legend of the Swordsman.mp4";
  23. //ossObject must be RFC3986-encoded
  24. String rfc3986Object = encodeByRFC3986(ossObject);
  25. request.setFileURLs(ossHost + rfc3986Object);
  26. QueryMediaListByURLResponse response = this.client.getAcsResponse(request);
  27. System.out.println(JSONObject.toJSONString(response.getMediaList()));
  28. }
  29. private String encodeByRFC3986(String object) throws UnsupportedEncodingException {
  30. StringBuilder builder = new StringBuilder();
  31. String[] segments = object.split("/");
  32. for (int i = 0; i < segments.length; i++) {
  33. builder.append(percentEncode(segments[i]));
  34. if (i != segments.length - 1) {
  35. builder.append("/");
  36. }
  37. }
  38. return builder.toString();
  39. }
  40. private static String percentEncode(String value) throws UnsupportedEncodingException {
  41. if (value == null)
  42. return null;
  43. return URLEncoder.encode(value, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
  44. }
  45. public static void main(String[] args) {
  46. try {
  47. QueryMediaListByURLDemo demo = new QueryMediaListByURLDemo();
  48. demo.queryMediaListByURL();
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. }
Thank you! We've received your feedback.