點播服務端提供豐富的上傳介面,支援開發人員採用不同的上傳方式實現媒體上傳。本文介紹Java SDK調用服務端上傳介面的使用情境和調用樣本。
使用情境
本文僅提供API調用樣本,即在點播服務中擷取上傳憑證和地址等操作樣本。要實現完整的媒體檔案上傳邏輯,請參考下表跳轉至對應的上傳方式和使用情境。
介面 | 使用情境 |
| |
|
前提條件
介面調用說明
本文提供的介面調用樣本均通過AccessKey初始化用戶端執行個體。
介面的參數解釋和返回欄位的詳細說明請訪問阿里雲OpenAPI門戶,在各介面右側的文檔頁簽查看。
本文僅提供部分複雜介面的程式碼範例,其餘介面的SDK程式碼範例,可以通過阿里雲OpenAPI門戶擷取。訪問阿里雲OpenAPI門戶,在介面的左側參數配置頁簽,填寫需要的參數資訊並發起調用後,在右側的SDK樣本頁簽,選擇SDK版本,選擇目標語言,查看並下載範例程式碼。
本文均以V1.0版本的SDK為例進行介面調用,如需擷取V2.0版本的SDK樣本,請在通過阿里雲OpenAPI門戶擷取SDK樣本時,指定到對應的SDK版本。
介面調用樣本
擷取音/視頻上傳地址和憑證
調用CreateUploadVideo介面,完成擷取音/視頻上傳地址和憑證功能。
阿里雲OpenAPI門戶地址:CreateUploadVideo。
調用樣本如下:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
/**
*
* 音頻或視頻擷取上傳憑證的樣本
*/
public class AudioOrVideoCreateUploadDemo {
/**
* 讀取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 CreateUploadVideoResponse 擷取音/視頻上傳地址和憑證響應資料
* @throws Exception
*/
public static CreateUploadVideoResponse createUploadVideo(DefaultAcsClient client) throws Exception {
CreateUploadVideoRequest request = new CreateUploadVideoRequest();
request.setTitle("this is a sample");
request.setFileName("filename.mp4");
//UserData,使用者自訂設定參數,使用者需要單獨回調URL及資料透傳時設定(非必須)
//JSONObject userData = new JSONObject();
//UserData回調部分設定
//訊息回調設定,指定時以此為準,否則以全域設定的事件通知為準
//JSONObject messageCallback = new JSONObject();
//設定回調地址
//messageCallback.put("CallbackURL", "http://192.168.0.1/16");
//設定回調方式,預設為http
//messageCallback.put("CallbackType", "http");
//userData.put("MessageCallback", messageCallback.toJSONString());
//UserData透傳資料部分設定
//使用者自訂的擴充欄位,用於回調時透傳返回
//JSONObject extend = new JSONObject();
//extend.put("MyId", "user-defined-id");
//userData.put("Extend", extend.toJSONString());
//request.setUserData(userData.toJSONString());
return client.getAcsResponse(request);
}
/**
* 請求樣本
*/
public static void main(String[] argv) throws ClientException {
try {
DefaultAcsClient client = initVodClient();
CreateUploadVideoResponse response = new CreateUploadVideoResponse();
response = createUploadVideo(client);
System.out.print("VideoId = " + response.getVideoId() + "\n");
System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
System.out.print("RequestId = " + response.getRequestId() + "\n");
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
}
}
重新整理音/視頻上傳憑證
調用RefreshUploadVideo介面,完成重新整理音/視頻上傳憑證功能。
阿里雲OpenAPI門戶地址:RefreshUploadVideo。
調用樣本如下:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
/**
*
* 音頻或視頻重新整理上傳憑證的樣本
*/
public class AudioOrVideoRefreshUploadDemo {
/**
* 讀取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 RefreshUploadVideoResponse 重新整理音/視頻上傳憑證響應資料
* @throws Exception
*/
public static RefreshUploadVideoResponse refreshUploadVideo(DefaultAcsClient client) throws Exception {
RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
//音頻或視頻ID
request.setVideoId("<VideoId>");
return client.getAcsResponse(request);
}
/**
* 請求樣本
*/
public static void main(String[] argv) {
try {
DefaultAcsClient client = initVodClient();
RefreshUploadVideoResponse response = refreshUploadVideo(client);
System.out.print("VideoId = " + response.getVideoId() + "\n");
System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
System.out.print("RequestId = " + response.getRequestId() + "\n");
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
}
}
擷取圖片上傳地址和憑證
調用CreateUploadImage介面,完成擷取圖片上傳地址和憑證功能。
阿里雲OpenAPI門戶地址:CreateUploadImage。
調用樣本如下:
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.CreateUploadImageRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadImageResponse;
/**
* 圖片擷取上傳憑證的樣本
*
*/
public class ImageCreateUploadDemo {
/**
* 讀取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 CreateUploadImageResponse 擷取圖片上傳地址和憑證響應資料
* @throws Exception
*/
public static CreateUploadImageResponse createUploadImage(DefaultAcsClient client) throws Exception {
CreateUploadImageRequest request = new CreateUploadImageRequest();
//設定圖片類型
request.setImageType("default");
//設定圖片副檔名
request.setImageExt("gif");
//設定圖片標題
request.setTitle("this is a sample");
//UserData,使用者自訂設定參數,使用者需要單獨回調URL及資料透傳時設定(非必須)
JSONObject userData = new JSONObject();
//UserData回調部分設定
//訊息回調設定,指定時以此為準,否則以全域設定的事件通知為準
JSONObject messageCallback = new JSONObject();
//設定回調地址
messageCallback.put("CallbackURL", "http://192.168.0.0/16");
//設定回調方式,預設為http
messageCallback.put("CallbackType", "http");
userData.put("MessageCallback", messageCallback.toJSONString());
JSONObject extend = new JSONObject();
extend.put("MyId", "user-defined-id");
userData.put("Extend", extend.toJSONString());
request.setUserData(userData.toJSONString());
return client.getAcsResponse(request);
}
/**
* 請求樣本
*/
public static void main(String[] argv) {
try {
DefaultAcsClient client = initVodClient();
CreateUploadImageResponse response = createUploadImage(client);
System.out.print("ImageId = " + response.getImageId() + "\n");
System.out.print("ImageURL = " + response.getImageURL() + "\n");
System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
System.out.print("RequestId = " + response.getRequestId() + "\n");
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
}
}
擷取輔助媒資上傳地址和憑證
調用CreateUploadAttachedMedia介面,完成擷取輔助媒資上傳地址和憑證功能。
阿里雲OpenAPI門戶地址:CreateUploadAttachedMedia。
調用樣本如下:
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.CreateUploadAttachedMediaRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadAttachedMediaResponse;
/**
* 輔助媒資擷取上傳憑證的樣本
*
*/
public class AttachedMediaCreateUploadDemo {
/**
* 讀取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 CreateUploadAttachedMediaResponse 擷取輔助媒資上傳地址和憑證響應資料
* @throws Exception
*/
public static CreateUploadAttachedMediaResponse createUploadAttachedMedia(DefaultAcsClient client) throws Exception {
CreateUploadAttachedMediaRequest request = new CreateUploadAttachedMediaRequest();
//設定業務類型
request.setBusinessType("watermark");
//設定副檔名
request.setMediaExt("gif");
//設定標題
request.setTitle("this is a sample");
//UserData,使用者自訂設定參數,使用者需要單獨回調URL及資料透傳時設定(非必須)
JSONObject userData = new JSONObject();
//UserData回調部分設定
//訊息回調設定,指定時以此為準,否則以全域設定的事件通知為準
JSONObject messageCallback = new JSONObject();
//設定回調地址
messageCallback.put("CallbackURL", "http://192.168.0.0/16");
//設定回調類型,預設為http
messageCallback.put("CallbackType", "http");
userData.put("MessageCallback", messageCallback.toJSONString());
JSONObject extend = new JSONObject();
extend.put("MyId", "user-defined-id");
userData.put("Extend", extend.toJSONString());
request.setUserData(userData.toJSONString());
return client.getAcsResponse(request);
}
/**
* 請求樣本
*/
public static void main(String[] argv) {
try {
DefaultAcsClient client = initVodClient();
CreateUploadAttachedMediaResponse response = createUploadAttachedMedia(client);
System.out.print("mediaId = " + response.getMediaId() + "\n");
System.out.print("mediaURL = " + response.getMediaURL() + "\n");
System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
System.out.print("RequestId = " + response.getRequestId() + "\n");
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
}
}
URL批量拉取上傳
調用UploadMediaByURL介面,完成URL批量拉取上傳功能。
阿里雲OpenAPI門戶地址:UploadMediaByURL。
調用樣本如下:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.UploadMediaByURLRequest;
import com.aliyuncs.vod.model.v20170321.UploadMediaByURLResponse;
import java.net.URLEncoder;
/**
* URL批量拉取上傳功能樣本
*
*/
public class AudioOrVideoUploadByUrl {
/**
* 讀取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;
}
/**
* URL批量拉取上傳
*
* @param client 發送請求用戶端
* @return UploadMediaByURLResponse URL批量拉取上傳響應資料
* @throws Exception
*/
public static UploadMediaByURLResponse uploadMediaByURL(DefaultAcsClient client) throws Exception {
UploadMediaByURLRequest request = new UploadMediaByURLRequest();
String url = "http://video_01.mp4";
String encodeUrl = URLEncoder.encode(url, "UTF-8");
//視頻源檔案URL
request.setUploadURLs(encodeUrl);
//上傳視頻中繼資料資訊
JSONObject uploadMetadata = new JSONObject();
//需要上傳的視頻源檔案URL,與UploadURLs裡的URL匹配才會生效
uploadMetadata.put("SourceUrl", encodeUrl);
//視頻標題
uploadMetadata.put("Title", "upload by url sample");
JSONArray uploadMetadataList = new JSONArray();
uploadMetadataList.add(uploadMetadata);
request.setUploadMetadatas(uploadMetadataList.toJSONString());
//UserData,使用者自訂設定參數,使用者需要單獨回調URL及資料透傳時設定(非必須)
JSONObject userData = new JSONObject();
//UserData回調部分設定
//訊息回調設定,指定時以此為準,否則以全域設定的事件通知為準
JSONObject messageCallback = new JSONObject();
//設定回調地址
messageCallback.put("CallbackURL", "http://192.168.0.0/16");
//設定回調類型,預設為http
messageCallback.put("CallbackType", "http");
userData.put("MessageCallback", messageCallback.toJSONString());
JSONObject extend = new JSONObject();
extend.put("MyId", "user-defined-id");
userData.put("Extend", extend.toJSONString());
request.setUserData(userData.toJSONString());
return client.getAcsResponse(request);
}
/**
* 請求樣本
*/
public static void main(String[] argv) {
try {
DefaultAcsClient client = initVodClient();
UploadMediaByURLResponse response = uploadMediaByURL(client);
System.out.print("UploadJobs = " + JSON.toJSONString(response.getUploadJobs()) + "\n");
System.out.print("RequestId = " + response.getRequestId() + "\n");
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
}
}
註冊媒資資訊
調用RegisterMedia介面,完成註冊媒資資訊功能。
阿里雲OpenAPI門戶地址:RegisterMedia。
調用樣本如下:
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.RegisterMediaRequest;
import com.aliyuncs.vod.model.v20170321.RegisterMediaResponse;
/**
* 媒資註冊樣本
*
*/
public class MediaRegisterDemo {
/**
* 讀取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 RegisterMediaResponse 註冊媒資資訊響應資料
* @throws Exception
*/
public static RegisterMediaResponse registerMedia(DefaultAcsClient client) throws Exception {
RegisterMediaRequest request = new RegisterMediaRequest();
JSONArray metaDataArray = new JSONArray();
JSONObject metaData = new JSONObject();
//標題
metaData.put("Title", "this is a sample");
//源檔案地址,檔案名稱需要保證全域唯一。添加相同檔案名稱時關聯唯一媒體ID。
metaData.put("FileURL", "https://192.168.0.0/16.oss-cn-shanghai.aliyuncs.com/vod_sample.mp4");
//指定要註冊的媒資中繼資料
metaDataArray.add((metaData));
request.setRegisterMetadatas(metaDataArray.toJSONString());
return client.getAcsResponse(request);
}
/**
* 請求樣本
*/
public static void main(String[] argv) {
try {
DefaultAcsClient client = initVodClient();
RegisterMediaResponse response = registerMedia(client);
if (response.getFailedFileURLs() != null && response.getFailedFileURLs().size() > 0) {
for (String fileURL : response.getFailedFileURLs()) {
System.out.print("FailedFileURL = " + fileURL + "\n");
}
}
if (response.getRegisteredMediaList() != null && response.getRegisteredMediaList().size() > 0) {
for (RegisterMediaResponse.RegisteredMedia registeredMedia : response.getRegisteredMediaList()) {
System.out.print("MediaId = " + registeredMedia.getMediaId());
System.out.print("FileURL = " + registeredMedia.getFileURL());
System.out.print("NewRegister = " + registeredMedia.getNewRegister());
System.out.print("RequestId = " + response.getRequestId() + "\n");
}
}
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
}
}
擷取URL上傳資訊
調用GetURLUploadInfos介面,完成擷取URL上傳資訊功能。
阿里雲OpenAPI門戶地址:GetURLUploadInfos。
調用樣本如下:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetURLUploadInfosRequest;
import com.aliyuncs.vod.model.v20170321.GetURLUploadInfosResponse;
import org.apache.commons.lang3.StringUtils;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
/**
* 擷取URL上傳資訊樣本
*/
public class URLUploadInfosGetDemo {
/**
* 讀取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;
}
/**
* 擷取URL上傳資訊
*
* @param client 發送請求用戶端
* @return GetURLUploadInfosResponse 擷取URL上傳資訊響應資料
* @throws Exception
*/
public static GetURLUploadInfosResponse getURLUploadInfos(DefaultAcsClient client) throws Exception {
GetURLUploadInfosRequest request = new GetURLUploadInfosRequest();
//上傳源視頻URL列表,需要URLencode
String[] urls = {
"http://exampleBucket****.cn-shanghai.aliyuncs.com/video_01.mp4",
"http://exampleBucket****.cn-shanghai.aliyuncs.com/video_02.flv"
};
List<String> encodeUrlList = new ArrayList<String>();
for (String url : urls) {
encodeUrlList.add(URLEncoder.encode(url, "UTF-8"));
}
request.setUploadURLs(StringUtils.join(encodeUrlList, ','));
//JobId列表,可以通過GetPlayInfo介面中返回的PlayInfo結構體中擷取
//request.setJobIds("exampleID1,exampleID2");
return client.getAcsResponse(request);
}
/**
* 請求樣本
*/
public static void main(String[] argv) {
try {
DefaultAcsClient client = initVodClient();
GetURLUploadInfosResponse response = getURLUploadInfos(client);
System.out.print("URLUploadInfoList = " + response.getURLUploadInfoList() + "\n");
System.out.print("RequestId = " + response.getRequestId() + "\n");
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
}
}