全部产品
云市场

媒体上传

更新时间:2019-01-31 17:22:24

本文档为Java版获取上传地址和凭证、注册媒资信息等服务端API的调用示例,要实现完整的媒体文件上传,可配合 客户端上传,也可以直接使用 服务端Java上传SDK

初始化客户端

使用前请先初始化客户端,参考 initVodClient 接口。

获取视频上传地址和凭证

接口参数和返回字段请参考 CreateUploadVideo

  1. import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
  2. import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
  3. /**
  4. * 获取视频上传地址和凭证
  5. * @param client 发送请求客户端
  6. * @return CreateUploadVideoResponse 获取视频上传地址和凭证响应数据
  7. * @throws Exception
  8. */
  9. public static CreateUploadVideoResponse createUploadVideo(DefaultAcsClient client) throws Exception {
  10. CreateUploadVideoRequest request = new CreateUploadVideoRequest();
  11. request.setTitle("this is a sample");
  12. request.setFileName("filename.mp4");
  13. JSONObject userData = new JSONObject();
  14. JSONObject messageCallback = new JSONObject();
  15. messageCallback.put("CallbackURL", "http://xxxxx");
  16. messageCallback.put("CallbackType", "http");
  17. userData.put("MessageCallback", messageCallback.toJSONString());
  18. JSONObject extend = new JSONObject();
  19. extend.put("MyId", "user-defined-id");
  20. userData.put("Extend", extend.toJSONString());
  21. request.setUserData(userData.toJSONString());
  22. return client.getAcsResponse(request);
  23. }
  24. // 请求示例
  25. public static void main(String[] argv) {
  26. DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
  27. CreateUploadVideoResponse response = new CreateUploadVideoResponse();
  28. try {
  29. response = createUploadVideo(client);
  30. System.out.print("VideoId = " + response.getVideoId() + "\n");
  31. System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
  32. System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
  33. } catch (Exception e) {
  34. System.out.print("ErrorMessage = " + e.getLocalizedMessage());
  35. }
  36. System.out.print("RequestId = " + response.getRequestId() + "\n");
  37. }

刷新视频上传凭证

接口参数和返回字段请参考 RefreshUploadVideo

  1. import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoRequest;
  2. import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
  3. /**
  4. * 刷新视频上传凭证
  5. * @param client 发送请求客户端
  6. * @return RefreshUploadVideoResponse 刷新视频上传凭证响应数据
  7. * @throws Exception
  8. */
  9. public static RefreshUploadVideoResponse refreshUploadVideo(DefaultAcsClient client) throws Exception {
  10. RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
  11. request.setVideoId("VideoId");
  12. return client.getAcsResponse(request);
  13. }
  14. // 请求示例
  15. public static void main(String[] argv) {
  16. DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
  17. RefreshUploadVideoResponse response = new RefreshUploadVideoResponse();
  18. try {
  19. response = refreshUploadVideo(client);
  20. System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
  21. System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
  22. } catch (Exception e) {
  23. System.out.print("ErrorMessage = " + e.getLocalizedMessage());
  24. }
  25. System.out.print("RequestId = " + response.getRequestId() + "\n");
  26. }

获取图片上传地址和凭证

接口参数和返回字段请参考 CreateUploadImage

  1. import com.aliyuncs.vod.model.v20170321.CreateUploadImageRequest;
  2. import com.aliyuncs.vod.model.v20170321.CreateUploadImageResponse;
  3. /**
  4. * 获取图片上传地址和凭证
  5. * @param client 发送请求客户端
  6. * @return CreateUploadImageResponse 获取图片上传地址和凭证响应数据
  7. * @throws Exception
  8. */
  9. public static CreateUploadImageResponse createUploadImage(DefaultAcsClient client) throws Exception {
  10. CreateUploadImageRequest request = new CreateUploadImageRequest();
  11. request.setImageType("default");
  12. request.setImageExt("gif");
  13. request.setTitle("this is a sample");
  14. JSONObject userData = new JSONObject();
  15. JSONObject messageCallback = new JSONObject();
  16. messageCallback.put("CallbackURL", "http://xxxxx");
  17. messageCallback.put("CallbackType", "http");
  18. userData.put("MessageCallback", messageCallback.toJSONString());
  19. JSONObject extend = new JSONObject();
  20. extend.put("MyId", "user-defined-id");
  21. userData.put("Extend", extend.toJSONString());
  22. request.setUserData(userData.toJSONString());
  23. return client.getAcsResponse(request);
  24. }
  25. // 请求示例
  26. public static void main(String[] argv) {
  27. DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
  28. CreateUploadImageResponse response = new CreateUploadImageResponse();
  29. try {
  30. response = createUploadImage(client);
  31. System.out.print("ImageId = " + response.getImageId() + "\n");
  32. System.out.print("ImageURL = " + response.getImageURL() + "\n");
  33. System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
  34. System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
  35. } catch (Exception e) {
  36. System.out.print("ErrorMessage = " + e.getLocalizedMessage());
  37. }
  38. System.out.print("RequestId = " + response.getRequestId() + "\n");
  39. }

获取辅助媒资上传地址和凭证

接口参数和返回字段请参考 CreateUploadAttachedMedia

  1. import com.aliyuncs.vod.model.v20170321.CreateUploadAttachedMediaRequest;
  2. import com.aliyuncs.vod.model.v20170321.CreateUploadAttachedMediaResponse;
  3. /**
  4. * 获取辅助媒资(水印、字幕等)上传地址和凭证
  5. * @param client 发送请求客户端
  6. * @return CreateUploadAttachedMediaResponse 获取辅助媒资上传地址和凭证响应数据
  7. * @throws Exception
  8. */
  9. public static CreateUploadAttachedMediaResponse createUploadAttachedMedia(DefaultAcsClient client) throws Exception {
  10. CreateUploadAttachedMediaRequest request = new CreateUploadAttachedMediaRequest();
  11. request.setBusinessType("watermark");
  12. request.setMediaExt("gif");
  13. request.setTitle("this is a sample");
  14. JSONObject userData = new JSONObject();
  15. JSONObject messageCallback = new JSONObject();
  16. messageCallback.put("CallbackURL", "http://xxxxx");
  17. messageCallback.put("CallbackType", "http");
  18. userData.put("MessageCallback", messageCallback.toJSONString());
  19. JSONObject extend = new JSONObject();
  20. extend.put("MyId", "user-defined-id");
  21. userData.put("Extend", extend.toJSONString());
  22. request.setUserData(userData.toJSONString());
  23. return client.getAcsResponse(request);
  24. }
  25. // 请求示例
  26. public static void main(String[] argv) {
  27. DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
  28. CreateUploadAttachedMediaResponse response = new CreateUploadAttachedMediaResponse();
  29. try {
  30. response = createUploadAttachedMedia(client);
  31. System.out.print("mediaId = " + response.getMediaId() + "\n");
  32. System.out.print("mediaURL = " + response.getMediaURL() + "\n");
  33. System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
  34. System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
  35. } catch (Exception e) {
  36. System.out.print("ErrorMessage = " + e.getLocalizedMessage());
  37. }
  38. System.out.print("RequestId = " + response.getRequestId() + "\n");
  39. }

URL批量拉取上传

接口参数和返回字段请参考 UploadMediaByURL

  1. import com.alibaba.fastjson.JSON;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.aliyuncs.vod.model.v20170321.UploadMediaByURLRequest;
  5. import com.aliyuncs.vod.model.v20170321.UploadMediaByURLResponse;
  6. /**
  7. * URL批量拉取上传
  8. * @param client 发送请求客户端
  9. * @return UploadMediaByURLResponse URL批量拉取上传响应数据
  10. * @throws Exception
  11. */
  12. public static UploadMediaByURLResponse uploadMediaByURL(DefaultAcsClient client) throws Exception {
  13. UploadMediaByURLRequest request = new UploadMediaByURLRequest();
  14. String url = "http://xxxx.mp4";
  15. String encodeUrl = URLEncoder.encode(url, "UTF-8");
  16. request.setUploadURLs(encodeUrl);
  17. JSONObject uploadMetadata = new JSONObject();
  18. uploadMetadata.put("SourceUrl", encodeUrl);
  19. uploadMetadata.put("Title", "upload by url sample");
  20. JSONArray uploadMetadataList = new JSONArray();
  21. uploadMetadataList.add(uploadMetadata);
  22. request.setUploadMetadatas(uploadMetadataList.toJSONString());
  23. JSONObject userData = new JSONObject();
  24. JSONObject messageCallback = new JSONObject();
  25. messageCallback.put("CallbackURL", "http://xxxxx");
  26. messageCallback.put("CallbackType", "http");
  27. userData.put("MessageCallback", messageCallback.toJSONString());
  28. JSONObject extend = new JSONObject();
  29. extend.put("MyId", "user-defined-id");
  30. userData.put("Extend", extend.toJSONString());
  31. request.setUserData(userData.toJSONString());
  32. return client.getAcsResponse(request);
  33. }
  34. // 请求示例
  35. public static void main(String[] argv) {
  36. DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
  37. UploadMediaByURLResponse response = new UploadMediaByURLResponse();
  38. try {
  39. response = uploadMediaByURL(client);
  40. System.out.print("UploadJobs = " + JSON.toJSONString(response.getUploadJobs()) + "\n");
  41. } catch (Exception e) {
  42. System.out.print("ErrorMessage = " + e.getLocalizedMessage());
  43. }
  44. System.out.print("RequestId = " + response.getRequestId() + "\n");
  45. }

注册媒资信息

接口参数和返回字段请参考 RegisterMedia

  1. import com.aliyuncs.vod.model.v20170321.RegisterMediaRequest;
  2. import com.aliyuncs.vod.model.v20170321.RegisterMediaResponse;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. /**
  6. * 注册媒资信息
  7. * @param client 发送请求客户端
  8. * @return RegisterMediaResponse 注册媒资信息响应数据
  9. * @throws Exception
  10. */
  11. public static RegisterMediaResponse registerMedia(DefaultAcsClient client) throws Exception {
  12. RegisterMediaRequest request = new RegisterMediaRequest();
  13. JSONArray metaDataArray = new JSONArray();
  14. JSONObject metaData = new JSONObject();
  15. metaData.put("Title", "this is a sample");
  16. metaData.put("FileURL", "https://xxxxxx.oss-cn-shanghai.aliyuncs.com/vod_sample.mp4");
  17. metaDataArray.add((metaData));
  18. request.setRegisterMetadatas(metaDataArray.toJSONString());
  19. return client.getAcsResponse(request);
  20. }
  21. // 请求示例
  22. public static void main(String[] argv) throws Exception {
  23. DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
  24. RegisterMediaResponse response = new RegisterMediaResponse();
  25. try {
  26. response = registerMedia(client);
  27. if (response.getFailedFileURLs() != null && response.getFailedFileURLs().size() > 0) {
  28. for (String fileURL : response.getFailedFileURLs()) {
  29. System.out.print("FailedFileURL = " + fileURL + "\n");
  30. }
  31. }
  32. if (response.getRegisteredMediaList() != null && response.getRegisteredMediaList().size() > 0) {
  33. for (RegisterMediaResponse.RegisteredMedia registeredMedia : response.getRegisteredMediaList()) {
  34. System.out.print("MediaId = " + registeredMedia.getMediaId());
  35. System.out.print("FileURL = " + registeredMedia.getFileURL());
  36. System.out.print("NewRegister = " + registeredMedia.getNewRegister());
  37. }
  38. }
  39. } catch (Exception e) {
  40. System.out.print("ErrorMessage = " + e.getLocalizedMessage());
  41. }
  42. System.out.print("RequestId = " + response.getRequestId() + "\n");
  43. }

获取URL上传信息

接口参数和返回字段请参考 GetURLUploadInfos

  1. import java.net.URLEncoder;
  2. import java.util.Arrays;
  3. import org.apache.commons.lang.StringUtils;
  4. import com.aliyuncs.DefaultAcsClient;
  5. import com.aliyuncs.profile.DefaultProfile;
  6. import com.aliyuncs.vod.model.v20170321.GetURLUploadInfosRequest;
  7. import com.aliyuncs.vod.model.v20170321.GetURLUploadInfosResponse;
  8. /**
  9. * 获取URL上传信息
  10. * @param client 发送请求客户端
  11. * @return GetURLUploadInfosResponse 获取URL上传信息响应数据
  12. * @throws Exception
  13. */
  14. public static GetURLUploadInfosResponse getURLUploadInfos(DefaultAcsClient client) throws Exception {
  15. GetURLUploadInfosRequest request = new GetURLUploadInfosRequest();
  16. String[] urls = {
  17. "http://xxx.cn-shanghai.aliyuncs.com/sample1.mp4",
  18. "http://xxx.cn-shanghai.aliyuncs.com/sample2.flv"
  19. };
  20. List<String> encodeUrlList = new ArrayList<String>();
  21. for(String url : urls){
  22. encodeUrlList.add(URLEncoder.encode(url, "UTF-8"));
  23. }
  24. request.setUploadURLs(StringUtils.join(encodeUrlList, ','));
  25. //request.setJobIds("xxx1,xxx2");
  26. return client.getAcsResponse(request);
  27. }
  28. // 请求示例
  29. public static void main(String[] argv) {
  30. DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
  31. GetURLUploadInfosResponse response = new GetURLUploadInfosResponse();
  32. try {
  33. response = getURLUploadInfos(client);
  34. } catch (Exception e) {
  35. System.out.print("ErrorMessage = " + e.getLocalizedMessage());
  36. }
  37. System.out.print("RequestId = " + response.getRequestId() + "\n");
  38. }