All Products
Search
Document Center

Java upload SDK

Last Updated: Dec 05, 2018

Environment requirements

Java 6+

SDK installation

The following example uses SDK 1.3.1 to describe the installation procedure:

1.1. Download the Java sample code VODUploadDemo-java-1.3.1.zip development package (including the sample code and required JAR packages). For more information, see Download the video upload SDK.

  1. Decompress the package and copy all the JAR files in the lib library to your project.

  2. The following shows the description of the edition of JAR packages that the SDK depends on.

Note: The editions of some core JAR packages are listed below. You can directly add a maven dependency to your project or add all JAR packages in the VODUploadDemo-java-1.3.1.zip package to your project.

  1. <dependency>
  2. <groupId>com.aliyun</groupId>
  3. <artifactId>aliyun-java-sdk-core</artifactId>
  4. <version>3.7.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.aliyun.oss</groupId>
  8. <artifactId>aliyun-sdk-oss</artifactId>
  9. <version>3.1.0</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.aliyun</groupId>
  13. <artifactId>aliyun-java-sdk-vod</artifactId>
  14. <version>2.11.0</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>com.alibaba</groupId>
  18. <artifactId>fastjson</artifactId>
  19. <version>1.2.28</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.json</groupId>
  23. <artifactId>json</artifactId>
  24. <version>20170516</version>
  25. </dependency>
  1. The following example shows how to add the JAR package for IDE development. This example uses Eclipse and IntelliJ IDEA.

    4.1 In Eclipse, right click your project and choose Properties > Java Build Path > Add JARs.

    4.2 In IntelliJ IDEA, choose File > Project Structure > Modules. Right click Dependencies and select JARs or directories.

  2. Select all JAR files that you have copied in the first step.

    When you complete the preceding steps, you can use the VODUpload Java SDK in Eclipse or IntelliJ IDEA.

Sample code

The following Java sample code shows how to upload a file from the server to ApsaraVideo VOD. Currently, the following four upload methods are supported:

    1. You can upload a local file by using multi-fragment load or resumable upload. For more information, see the testUploadVideo function.

    1.1 When resumable upload is disabled, the maximum upload task execution time is 3,000 seconds. The size of files to be uploaded depends on your network bandwidth and disk read/write capability.

    1.2 When resumable upload is enabled, you can upload a single file of up to 48.8 TB. After resumable upload is enabled, the current upload path is written to files on the local disk during the uploading process. This affects the upload speed. Select whether to enable resumable upload based on your file size.

    1. You can specify a file URL to upload a network stream. Resumable upload is not supported. The maximum size of a single file that can be uploaded is 5 GB. For more information, see the testUploadURLStream function.
    1. You can specify a local file to upload a file stream. Resumable upload is not supported. The maximum size of a single file that can be uploaded is 5 GB. For more information, see the testUploadFileStream function.
    1. In stream upload mode, you can specify an input stream for upload. File streams and network streams are supported. Resumable upload is not supported. The maximum size of a single file that can be uploaded is 5 GB. For more information, see the testUploadStream function.

Note: Replace the values of the required parameters in the sample with the actual values. Delete optional parameters that you do not need from the sample to ensure that the settings meet your requirements.

In the decompressed VODUploadDemo-java-1.3.1.zip development package,UploadVideoDemo.java in the sample directory is the sample program, as shown below:

  1. public class UploadVideoDemo {
  2. //Enter the account AccessKey (required)
  3. private static final String accessKeyId = "";
  4. //Enter the account AccessKey (required)
  5. private static final String accessKeySecret = "";
  6. public static void main(String[] args) {
  7. //The title of the video (required)
  8. String title = "test title";
  9. //1. In local file upload and file stream upload modes, the file name is the absolute path of the file to be uploaded, for example, /User/sample/file name.mp4 (required).
  10. //2. In network stream upload mode, the file name is the source file name, for example, file name.mp4 (required).
  11. //3. In stream upload mode, the file name is the source file name, for example, file name.mp4 (required).
  12. //4. In all upload modes, the file name must include an extension.
  13. String fileName = "test file name.mp4";
  14. //Uploads a local file.
  15. testUploadVideo(accessKeyId, accessKeySecret, title, fileName);
  16. //The address of the network stream to be uploaded
  17. String url = "http://video.sample.com/sample.mp4";
  18. //Uploads a network stream
  19. testUploadURLStream(accessKeyId, accessKeySecret, title, fileName, url);
  20. //Uploads a file stream
  21. testUploadFileStream(accessKeyId, accessKeySecret, title, fileName);
  22. //4. Uploads a stream, for example, a file stream or a network stream
  23. InputStream inputStream = null;
  24. //4.1 File stream
  25. // try {
  26. // inputStream = new FileInputStream(fileName);
  27. // } catch (FileNotFoundException e) {
  28. // e.printStackTrace();
  29. // }
  30. //4.2 Network stream
  31. // try {
  32. // inputStream = new URL(url).openStream();
  33. // } catch (IOException e) {
  34. // e.printStackTrace();
  35. // }
  36. testUploadStream(accessKeyId, accessKeySecret, title, fileName, inputStream);
  37. }
  38. /**
  39. * Local file upload operation
  40. * @param accessKeyId
  41. * @param accessKeySecret
  42. * @param title
  43. * @param fileName
  44. */
  45. private static void testUploadVideo(String accessKeyId, String accessKeySecret, String title, String fileName) {
  46. UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName);
  47. /* Specifies the size of each part in multi-fragment upload mode. The default size is 1 MB. */
  48. request.setPartSize(1 * 1024 * 1024L);
  49. /* Specifies the number of concurrent threads during multi-fragment upload. The default value is 1. Note: This configuration consumes CPU resource of the server. You can set a value based on your server.*/
  50. request.setTaskNum(1);
  51. /* Indicates whether to enable resumable upload. This function is disabled by default. When the same request is initiated after a network disconnection or program crash, the paused upload task can be resumed. It is applicable for large files that are not completely uploaded when the upload time has reached the maximum: 3,000 seconds.
  52. Note: After resumable upload is enabled, the current upload path is written to the files in local disk during the uploading process. This affects the upload speed. Select whether to enable resumable upload based on your actual requirements.*/
  53. request.setEnableCheckpoint(false);
  54. /* Specifies the OSS slow request timeout time in milliseconds. If it takes more than the specified timeout time to upload a fragment, a debug log is output. You can adjust the threshold to prevent outputting logs. The default value is 300,000 milliseconds.*/
  55. //request.setSlowRequestsThreshold(300000L);
  56. /* Specifies the slow request timeout time for a fragment. The default value is 300 seconds.*/
  57. //request.setSlowRequestsThreshold(300000L);
  58. /* Indicates whether to apply the default watermark (optional). You can choose whether to apply the default watermark when specifying the template ID based on the template configuration.*/
  59. //request.setIsShowWaterMark(true);
  60. /* Specifies the callback URL after upload is finished (optional). We recommend that you configure the event callback through the VOD console. For more information, see https://www.alibabacloud.com/help/doc-detail/92224.htm. */
  61. //request.setCallback("http://callback.sample.com");
  62. /* The category ID of the video (optional) */
  63. //request.setCateId(0);
  64. /* The tags of the video separated by commas (,) (optional). */
  65. //request.setTags("tag1,tag2");
  66. /* The description of the video (optional) */
  67. //request.setDescription("video description");
  68. /* The album art of the video (optional) */
  69. //request.setCoverURL("http://cover.sample.com/sample.jpg");
  70. /* The ID of the template (optional) */
  71. //request.setTemplateGroupId("8c4792cbc8694e7084fd5330e56a33d");
  72. /* The storage region (optional) */
  73. //request.setStorageLocation("in-201703232118266-5sejdln9o.oss-cn-shanghai.aliyuncs.com");
  74. UploadVideoImpl uploader = new UploadVideoImpl();
  75. UploadVideoResponse response = uploader.uploadVideo(request);
  76. System.out.print("RequestId=" + response.getRequestId() + "\n"); //The ID of the VOD request
  77. if (response.isSuccess()) {
  78. System.out.print("VideoId=" + response.getVideoId() + "\n");
  79. } else {
  80. /* If the callback URL you set is invalid, video upload is not affected, and the video ID and error code are returned. For upload failures in other cases, the video ID is empty. You should analyze the cause based on the returned error code. */
  81. System.out.print("VideoId=" + response.getVideoId() + "\n");
  82. System.out.print("ErrorCode=" + response.getCode() + "\n");
  83. System.out.print("ErrorMessage=" + response.getMessage() + "\n");
  84. }
  85. }
  86. /**
  87. * Network stream upload operation
  88. * @param accessKeyId
  89. * @param accessKeySecret
  90. * @param title
  91. * @param fileName
  92. * @param url
  93. */
  94. private static void testUploadURLStream(String accessKeyId, String accessKeySecret, String title, String fileName, String url) {
  95. UploadURLStreamRequest request = new UploadURLStreamRequest(accessKeyId, accessKeySecret, title, fileName, url);
  96. /* Indicates whether to apply the default watermark (optional). You can choose whether to apply the default watermark when specifying the template ID based on the template configuration.*/
  97. //request.setIsShowWaterMark(true);
  98. /* Specifies the callback URL after upload is finished (optional). We recommend that you configure the event callback through the VOD console. For more information, see https://www.alibabacloud.com/help/doc-detail/92224.htm. */
  99. //request.setCallback("http://callback.sample.com");
  100. /* The category ID of the video (optional) */
  101. //request.setCateId(0);
  102. /* The tags of the video separated with commas (,) (optional) */
  103. //request.setTags("tag1,tag2");
  104. /* The description of the video (optional) */
  105. //request.setDescription("video description");
  106. /* The album art of the video (optional) */
  107. //request.setCoverURL("http://cover.sample.com/sample.jpg");
  108. /* The template ID of the video (optional) */
  109. //request.setTemplateGroupId("8c4792cbc8694e7084fd5330e56a33d");
  110. /* The storage region (optional) */
  111. //request.setStorageLocation("in-201703232118266-5sejdln9o.oss-cn-shanghai.aliyuncs.com");
  112. UploadVideoImpl uploader = new UploadVideoImpl();
  113. UploadURLStreamResponse response = uploader.uploadURLStream(request);
  114. System.out.print("RequestId=" + response.getRequestId() + "\n"); //The ID of the VOD request
  115. if (response.isSuccess()) {
  116. System.out.print("VideoId=" + response.getVideoId() + "\n");
  117. } else {
  118. /* If the callback URL you set is invalid, video upload is not affected, and the video ID and error code are returned. For upload failures in other cases, the video ID is empty. You should analyze the cause based on the returned error code. */
  119. System.out.print("VideoId=" + response.getVideoId() + "\n");
  120. System.out.print("ErrorCode=" + response.getCode() + "\n");
  121. System.out.print("ErrorMessage=" + response.getMessage() + "\n");
  122. }
  123. }
  124. /**
  125. * File stream upload operation
  126. * @param accessKeyId
  127. * @param accessKeySecret
  128. * @param title
  129. * @param fileName
  130. */
  131. private static void testUploadFileStream(String accessKeyId, String accessKeySecret, String title, String fileName) {
  132. UploadFileStreamRequest request = new UploadFileStreamRequest(accessKeyId, accessKeySecret, title, fileName);
  133. /* Indicates whether to apply the default watermark (optional). You can choose whether to apply the default watermark when specifying the template ID based on the template configuration.*/
  134. //request.setShowWaterMark(true);
  135. /* Specifies the callback URL after upload is finished (optional). We recommend that you configure the event callback through the VOD console. For more information, see https://www.alibabacloud.com/help/doc-detail/92224.htm. */
  136. //request.setCallback("http://callback.sample.com");
  137. /* The category ID of the video (optional) */
  138. //request.setCateId(0);
  139. /* The tags of the video separated with commas (,) (optional). */
  140. //request.setTags("tag1,tag2");
  141. /* The description of the video (optional) */
  142. //request.setDescription("video description");
  143. /* The album art of the video (optional) */
  144. //request.setCoverURL("http://cover.sample.com/sample.jpg");
  145. /* The template ID (optional) */
  146. //request.setTemplateGroupId("8c4792cbc8694e7084fd5330e56a33d");
  147. /* The storage region (optional) */
  148. //request.setStorageLocation("in-201703232118266-5sejdln9o.oss-cn-shanghai.aliyuncs.com");
  149. UploadVideoImpl uploader = new UploadVideoImpl();
  150. UploadFileStreamResponse response = uploader.uploadFileStream(request);
  151. System.out.print("RequestId=" + response.getRequestId() + "\n"); //The ID of the VOD request
  152. if (response.isSuccess()) {
  153. System.out.print("VideoId=" + response.getVideoId() + "\n");
  154. } else {
  155. /* If the callback URL you set is invalid, video upload is not affected, and the video ID and error code are returned. For upload failures in other cases, the video ID is empty. You should analyze the cause based on the returned error code. */
  156. System.out.print("VideoId=" + response.getVideoId() + "\n");
  157. System.out.print("ErrorCode=" + response.getCode() + "\n");
  158. System.out.print("ErrorMessage=" + response.getMessage() + "\n");
  159. }
  160. }
  161. /**
  162. * Stream upload operation
  163. *
  164. * @param accessKeyId
  165. * @param accessKeySecret
  166. * @param title
  167. * @param fileName
  168. * @param inputStream
  169. */
  170. private static void testUploadStream(String accessKeyId, String accessKeySecret, String title, String fileName, InputStream inputStream) {
  171. UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
  172. /* Indicates whether to apply the default watermark (optional). You can choose whether to apply the default watermark when specifying the template ID based on the template configuration.*/
  173. //request.setShowWaterMark(true);
  174. /* Specifies the callback URL after upload is finished (optional). We recommend that you configure the event callback through the VOD console. For more information, see https://www.alibabacloud.com/help/doc-detail/92224.htm. */
  175. //request.setCallback("http://callback.sample.com");
  176. /* The category ID of the video (optional) */
  177. //request.setCateId(0);
  178. /* The tags of the video separated with commas (,) (optional) */
  179. //request.setTags("tag1,tag2");
  180. /* The description of the video (optional) */
  181. //request.setDescription("video description");
  182. /* The album art of the video (optional) */
  183. //request.setCoverURL("http://cover.sample.com/sample.jpg");
  184. /* The template ID (optional) */
  185. //request.setTemplateGroupId("8c4792cbc8694e7084fd5330e56a33d");
  186. /* The storage region (optional) */
  187. //request.setStorageLocation("in-201703232118266-5sejdln9o.oss-cn-shanghai.aliyuncs.com");
  188. UploadVideoImpl uploader = new UploadVideoImpl();
  189. UploadStreamResponse response = uploader.uploadStream(request);
  190. System.out.print("RequestId=" + response.getRequestId() + "\n"); //The ID of the VOD request
  191. if (response.isSuccess()) {
  192. System.out.print("VideoId=" + response.getVideoId() + "\n");
  193. } else { //If the callback URL you set is invalid, video upload is not affected, and the video ID and error code are returned. For upload failures in other cases, the video ID is empty. You should analyze the cause based on the returned error code.
  194. System.out.print("VideoId=" + response.getVideoId() + "\n");
  195. System.out.print("ErrorCode=" + response.getCode() + "\n");
  196. System.out.print("ErrorMessage=" + response.getMessage() + "\n");
  197. }
  198. }
  199. }