If you do not know the ID of the media file, such as in live-to-VOD scenarios, you can call the QueryMediaListByURL operation to query a media file by using its Object Storage Service (OSS) URL. Sample code:
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.mts.model.v20140618.QueryMediaListByURLRequest;
import com.aliyuncs.mts.model.v20140618.QueryMediaListByURLResponse;
import com.aliyuncs.profile.DefaultProfile;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class QueryMediaListByURLDemo {
// Query the information about a media file by using its OSS URL. For example, you can query the ID, status, and other attributes of the media file.
private void queryMediaListByURL() throws ClientException, UnsupportedEncodingException {
// Initialize the SDK client.
DefaultAcsClient client = InitClient.initMpsClient();
QueryMediaListByURLRequest request = new QueryMediaListByURLRequest();
String ossHost = 'http://<input-bucket>.<region>.aliyuncs.com/';
String ossObject = "test/video.mp4";
// Encode the OSS URL according to the RFC3986 standard.
String rfc3986Object = encodeByRFC3986(ossObject);
request.setFileURLs(ossHost + rfc3986Object);
QueryMediaListByURLResponse response = client.getAcsResponse(request);
System.out.println(JSONObject.toJSONString(response.getMediaList()));
}
private String encodeByRFC3986(String object) throws UnsupportedEncodingException {
StringBuilder builder = new StringBuilder();
String[] segments = object.split("/");
for (int i = 0; i < segments.length; i++) {
builder.append(percentEncode(segments[i]));
if (i != segments.length - 1) {
builder.append("/");
}
}
return builder.toString();
}
private static String percentEncode(String value) throws UnsupportedEncodingException {
if (value == null)
return null;
return URLEncoder.encode(value, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
}
public static void main(String[] args) {
try {
QueryMediaListByURLDemo demo = new QueryMediaListByURLDemo();
demo.queryMediaListByURL();
} catch (Exception e) {
e.printStackTrace();
}
}
}