本篇文檔提供了Java SDK媒資管理模組相關功能的API調用樣本。主要包含搜尋媒資資訊、擷取視頻資訊、修改視頻資訊、刪除視頻、擷取源檔案資訊、擷取圖片資訊、刪除圖片資訊等。
介面調用說明
本文提供的介面調用樣本均通過AccessKey初始化用戶端執行個體。
介面的參數解釋和返回欄位的詳細說明請訪問阿里雲OpenAPI門戶,在各介面右側的文檔頁簽查看。
本文僅提供部分複雜介面的程式碼範例,其餘介面的SDK程式碼範例,可以通過阿里雲OpenAPI門戶擷取。訪問阿里雲OpenAPI門戶,在介面的左側參數配置頁簽,填寫需要的參數資訊並發起調用後,在右側的SDK樣本頁簽,選擇SDK版本,選擇目標語言,查看並下載範例程式碼。
本文均以V1.0版本的SDK為例進行介面調用,如需擷取V2.0版本的SDK樣本,請在通過阿里雲OpenAPI門戶擷取SDK樣本時,指定到對應的SDK版本。

初始化用戶端
使用前請先初始化用戶端,請參見初始化。
搜尋媒資資訊
調用SearchMedia介面,產生搜尋媒資資訊的SDK樣本。
阿里雲OpenAPI門戶地址:SearchMedia。
調用樣本如下:
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.SearchMediaRequest;
import com.aliyuncs.vod.model.v20170321.SearchMediaResponse;
/**
*讀取AccessKey資訊
*/
public static DefaultAcsClient initVodClient() throws ClientException {
String regionId = "cn-shanghai"; // 點播服務接入地區
// 阿里雲帳號AccessKey擁有所有API的存取權限,建議您使用RAM使用者進行API訪問或日常營運。
// 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。
// 本樣本通過從環境變數中讀取AccessKey,來實現API訪問的身分識別驗證。運行程式碼範例前,請配置環境變數ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET。
DefaultProfile profile = DefaultProfile.getProfile(regionId, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
/**
* 搜尋媒資資訊
* @param client 發送請求用戶端
* @return SearchMediaResponse 搜尋媒資資訊響應資料
* @throws Exception
*/
public static SearchMediaResponse searchMedia(DefaultAcsClient client) throws Exception {
SearchMediaRequest request = new SearchMediaRequest();
request.setFields("Title,CoverURL,Status");
request.setMatch("Status in ('Normal','Checking') and CreationTime = ('2018-07-01T08:00:00Z','2018-08-01T08:00:00Z')");
request.setPageNo(1);
request.setPageSize(10);
request.setSearchType("video");
request.setSortBy("CreationTime:Desc");
return client.getAcsResponse(request);
}
// 請求樣本
public static void main(String[] argv) {
DefaultAcsClient client = initVodClient();
SearchMediaResponse response = new SearchMediaResponse();
try {
response = searchMedia(client);
if (response.getMediaList() != null && response.getMediaList().size() > 0) {
System.out.print("Total = " + response.getTotal() + "\n");
System.out.print("ScrollToken = " + response.getScrollToken() + "\n");
for (SearchMediaResponse.Media media : response.getMediaList()) {
System.out.print("MediaId = " + media.getMediaId() + "\n");
System.out.print("MediaType = " + media.getMediaType() + "\n");
System.out.print("CreationTime = " + media.getCreationTime() + "\n");
System.out.print("Title = " + media.getVideo().getTitle() + "\n");
System.out.print("CoverURL = " + media.getVideo().getCoverURL() + "\n");
System.out.print("Status = " + media.getVideo().getStatus() + "\n");
}
}
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.print("RequestId = " + response.getRequestId() + "\n");
}擷取音視頻資訊
調用GetVideoInfo介面,產生擷取單個音視頻的資訊的SDK樣本。
阿里雲OpenAPI門戶地址:GetVideoInfo。
調用GetVideoInfos介面,產生批量擷取音視頻的資訊的SDK樣本。
阿里雲OpenAPI門戶地址:GetVideoInfos。
修改單個音視頻資訊
調用UpdateVideoInfo介面,產生修改單個音視頻資訊的SDK樣本。
阿里雲OpenAPI門戶地址:UpdateVideoInfo。
批量修改音視頻資訊
調用UpdateVideoInfos介面,產生批量修改音視頻資訊的SDK樣本。
阿里雲OpenAPI門戶地址:UpdateVideoInfos。
調用樣本如下:
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.UpdateVideoInfosRequest;
import com.aliyuncs.vod.model.v20170321.UpdateVideoInfosResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
*讀取AccessKey資訊
*/
public static DefaultAcsClient initVodClient() throws ClientException {
String regionId = "cn-shanghai"; // 點播服務接入地區
// 阿里雲帳號AccessKey擁有所有API的存取權限,建議您使用RAM使用者進行API訪問或日常營運。
// 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。
// 本樣本通過從環境變數中讀取AccessKey,來實現API訪問的身分識別驗證。運行程式碼範例前,請配置環境變數ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET。
DefaultProfile profile = DefaultProfile.getProfile(regionId, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
/**
* 批量修改視頻資訊
* @param client 發送請求用戶端
* @return UpdateVideoInfosResponse 批量修改視頻資訊響應資料
* @throws Exception
*/
public static UpdateVideoInfosResponse updateVideoInfos(DefaultAcsClient client) throws Exception {
UpdateVideoInfosRequest request = new UpdateVideoInfosRequest();
JSONArray updateContentArray = new JSONArray();
JSONObject updateContent1 = new JSONObject();
updateContent1.put("VideoId", "VideoId1");
// updateContent1.put("Title", "new Title");
// updateContent1.put("Tags", "new Tag1,new Tag2");
updateContentArray.add((updateContent1));
JSONObject updateContent2 = new JSONObject();
updateContent2.put("VideoId", "VideoId2");
// updateContent2.put("Title", "new Title");
// updateContent2.put("Tags", "new Tag1,new Tag2");
updateContentArray.add((updateContent2));
request.setUpdateContent(updateContentArray.toJSONString());
return client.getAcsResponse(request);
}
// 請求樣本
public static void main(String[] argv) {
DefaultAcsClient client = initVodClient();
UpdateVideoInfosResponse response = new UpdateVideoInfosResponse();
try {
response = updateVideoInfos(client);
if (response.getNonExistVideoIds() != null && response.getNonExistVideoIds().size() > 0) {
System.out.print("======nonexistent VideoIds : ======\n");
for (String videoId : response.getNonExistVideoIds()) {
System.out.print(videoId + "\n");
}
}
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.print("RequestId = " + response.getRequestId() + "\n");
}擷取源檔案資訊(含源片下載地址)
調用GetMezzanineInfo介面,產生擷取源檔案資訊的SDK樣本。
阿里雲OpenAPI門戶地址:GetMezzanineInfo。
擷取音視頻列表
調用GetVideoList介面,產生擷取音視頻列表的SDK樣本。
阿里雲OpenAPI門戶地址:GetVideoList。
刪除音視頻
調用DeleteVideo介面,產生刪除音視頻的SDK樣本。
阿里雲OpenAPI門戶地址:DeleteVideo。
刪除媒體流
調用DeleteStream介面,產生刪除媒體流的SDK樣本。
阿里雲OpenAPI門戶地址:DeleteStream。
大量刪除源檔案
調用DeleteMezzanines介面,產生大量刪除源檔案的SDK樣本。
阿里雲OpenAPI門戶地址:DeleteMezzanines。
批次更新圖片資訊
調用UpdateImageInfos介面,產生批次更新圖片資訊的SDK樣本。
阿里雲OpenAPI門戶地址:UpdateImageInfos。
調用樣本如下:
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.UpdateImageInfosRequest;
import com.aliyuncs.vod.model.v20170321.UpdateImageInfosResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
*讀取AccessKey資訊
*/
public static DefaultAcsClient initVodClient() throws ClientException {
String regionId = "cn-shanghai"; // 點播服務接入地區
// 阿里雲帳號AccessKey擁有所有API的存取權限,建議您使用RAM使用者進行API訪問或日常營運。
// 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。
// 本樣本通過從環境變數中讀取AccessKey,來實現API訪問的身分識別驗證。運行程式碼範例前,請配置環境變數ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET。
DefaultProfile profile = DefaultProfile.getProfile(regionId, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
/**
* 批次更新圖片資訊函數
* @param client 發送請求用戶端
* @return UpdateImageInfosResponse 批次更新圖片資訊響應資料
* @throws Exception
*/
public static UpdateImageInfosResponse updateImageInfos(DefaultAcsClient client) throws Exception{
UpdateImageInfosRequest request = new UpdateImageInfosRequest();
JSONArray updateContentArray = new JSONArray();
JSONObject updateContent1 = new JSONObject();
updateContent1.put("ImageId", "ImageId1");
// updateContent1.put("Title", "new Title");
// updateContent1.put("Tags", "new Tag1,new Tag2");
updateContentArray.add((updateContent1));
JSONObject updateContent2 = new JSONObject();
updateContent2.put("ImageId", "ImageId2");
// updateContent2.put("Title", "new Title");
// updateContent2.put("Tags", "new Tag1,new Tag2");
updateContentArray.add((updateContent2));
request.setUpdateContent(updateContentArray.toJSONString());
return client.getAcsResponse(request);
}
// 請求樣本
public static void main(String[] argv) throws Exception {
DefaultAcsClient client = initVodClient();
UpdateImageInfosResponse response = new UpdateImageInfosResponse();
try {
response = updateImageInfos(client);
if (response.getNonExistImageIds() != null && response.getNonExistImageIds().size() > 0) {
System.out.print("======nonexistent ImageIds : ======\n");
for (String imageId : response.getNonExistImageIds()) {
System.out.print(imageId + "\n");
}
}
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.print("RequestId = " + response.getRequestId() + "\n");
}擷取圖片資訊
調用GetImageInfo介面,產生擷取圖片資訊的SDK樣本。
阿里雲OpenAPI門戶地址:GetImageInfo。
刪除圖片
調用DeleteImage介面,產生刪除圖片的SDK樣本。
阿里雲OpenAPI門戶地址:DeleteImage。
調用樣本如下:
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.DeleteImageRequest;
import com.aliyuncs.vod.model.v20170321.DeleteImageResponse;
/**
*讀取AccessKey資訊
*/
public static DefaultAcsClient initVodClient() throws ClientException {
String regionId = "cn-shanghai"; // 點播服務接入地區
// 阿里雲帳號AccessKey擁有所有API的存取權限,建議您使用RAM使用者進行API訪問或日常營運。
// 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。
// 本樣本通過從環境變數中讀取AccessKey,來實現API訪問的身分識別驗證。運行程式碼範例前,請配置環境變數ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET。
DefaultProfile profile = DefaultProfile.getProfile(regionId, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
/**
* 刪除圖片函數
*
* @param client 發送請求用戶端
* @return DeleteImageResponse 刪除圖片響應資料
* @throws Exception
*/
public static DeleteImageResponse deleteImage(DefaultAcsClient client) throws Exception {
DeleteImageRequest request = new DeleteImageRequest();
//根據ImageURL刪除圖片檔案
request.setDeleteImageType("ImageURL");
//ImageURL樣本:http://example.aliyundoc.com/cover-****.jpg
String url = "<your image URL>";
String encodeUrl = URLEncoder.encode(url, "UTF-8");
request.setImageURLs(encodeUrl);
//根據ImageId刪除圖片檔案
//request.setDeleteImageType("ImageId");
//request.setImageIds("ImageId1,ImageId2");
//根據VideoId刪除指定ImageType的圖片檔案
//request.setDeleteImageType("VideoId");
//request.setVideoId("VideoId");
//request.setImageType("SpriteSnapshot");
return client.getAcsResponse(request);
}
// 請求樣本
public static void main(String[] argv) throws ClientException {
DefaultAcsClient client = initVodClient();
DeleteImageResponse response = new DeleteImageResponse();
try {
response = deleteImage(client);
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.print("RequestId = " + response.getRequestId() + "\n");
}