All Products
Search
Document Center

ApsaraVideo VOD:Upload a short video

Last Updated:Dec 22, 2023

You can use the upload SDK to upload a short video file from a client to ApsaraVideo VOD. The videoId parameter is returned after the short video is uploaded by using the upload SDK. If ApsaraVideo Player is integrated, the video ID can be used for playback based on vid and playauth. This way, you do not need to obtain the playback URL and credential. This topic describes how to upload a short video by using the upload SDK for Android.

Background information

VODSVideoUploadClient is an upload instance that is used to upload short videos from an Android client. You can upload a short video and a thumbnail at the same time. You can use only a Security Token Service (STS) token to upload a short video. If you want to upload media files by using upload URLs and credentials, you must separately upload the video and the thumbnail by using VODUploadClient. For more information, see Upload a file.

Upload process

Process of uploading a media file by using an STS token

If you use authorization based on the STS token, you must create a RAM role and grant the RAM role permissions to access ApsaraVideo VOD. For more information, see Use STS to upload videos.

The following figure shows the complete process of uploading a media file by using an STS token.客户端STS方式上传流程

  1. A user deploys an authorization service on an application server, such as ApsaraVideo VOD SDK, to obtain temporary STS tokens.

  2. A client sends a request to the application server to request an STS token.

  3. The application server sends a request to STS to request the STS token.

  4. STS returns the token.

  5. The application server returns the STS token to the client.

  6. The client uses the STS token to initialize an upload instance.

  7. The client constructs upload parameters to send an upload request.

  8. OSS returns the upload result.

    Note

    You can also set callbacks in advance to receive notifications about upload events.

Upload a file

  1. Obtain an STS token. For more information, see Obtain an STS token.

  2. Declare a VODSVideoUploadClient object. The object cannot be a local variable.

    // Initialize the VODSVideoUploadClient object.
    VODSVideoUploadClient vodsVideoUploadClient = new VODSVideoUploadClientImpl(this.getApplicationContext());
    vodsVideoUploadClient.init();
  3. Construct upload parameters.

    Show code

    // Construct upload parameters.
    // Make sure that the parameter settings are valid. Otherwise, an exception is thrown in the SDK.
    // Make sure that the path of the file to be uploaded is valid. In addition, you must obtain the read and write permissions on the file. You can only dynamically obtain permissions in Android 6.0 or later.
    VodHttpClientConfig vodHttpClientConfig = new VodHttpClientConfig.Builder()
            .setMaxRetryCount(2)// Specify the maximum number of retries.
            .setConnectionTimeout(15 * 1000)// Specify the connection timeout period.
            .setSocketTimeout(15 * 1000)// Specify the timeout period for the socket connection.
            .build();
     // Construct the information about the short video, including the description, title, and details.
     SvideoInfo svideoInfo = new SvideoInfo();
     svideoInfo.setTitle(new File(videoPath).getName());// Specify the title.
     svideoInfo.setDesc("");// Specify the description.
     svideoInfo.setCateId(1);// Specify the category ID.
     // Construct parameters for uploading the short video to ApsaraVideo VOD. This step is important.
     VodSessionCreateInfo vodSessionCreateInfo =new    VodSessionCreateInfo.Builder()
    .setImagePath(imagePath)// Specify the path of the thumbnail.
    .setVideoPath(videoPath)// Specify the path of the video.
    .setAccessKeyId(accessKeyId)// Specify a temporary AccessKey ID.
    .setAccessKeySecret(accessKeySecret)// Specify a temporary AccessKey secret.
    .setSecurityToken(securityToken)// Specify the STS token.
    .setExpriedTime(expriedTime)// Specify the expiration time for the STS token.
    .setRequestID(requestID)// Specify the request ID. You can determine whether to obtain the request ID returned by STS.
    .setIsTranscode(true)// Specify whether to enable transcoding. If transcoding is enabled, you must register a listener on the AppServer to receive notifications when the transcoding succeeds on the ApsaraVideo VOD server.
    .setSvideoInfo(svideoInfo)// Specify the information about the short video.
    .setVodHttpClientConfig(vodHttpClientConfig)// Specify network parameters.
                            .build();
  4. Set callbacks and start the upload.

    You must set the VODSVideoUploadCallback callback before you start the upload.

    Show code

    vodsVideoUploadClient.uploadWithVideoAndImg(vodSessionCreateInfo, new VODSVideoUploadCallback() {
                        @Override
                        public void onUploadSucceed(String videoId, String imageUrl) {
                        // This callback is fired when the upload succeeds. The video ID and image URL are returned.
                        Log.d(TAG,"onUploadSucceed"+ "videoId:"+ videoId + "imageUrl" + imageUrl);
                        }
                        @Override
                        public void onUploadFailed(String code, String message) {
                            // This callback is fired when the upload fails. The error code and error message are returned. You must carefully read the error code and error message.
                            Log.d(TAG,"onUploadFailed" + "code" + code + "message" + message);
                        }
                        @Override
                        public void onUploadProgress(long uploadedSize, long totalSize) {
                            // This callback is fired when the default or custom upload progress is reached. The current thread is not a UI thread.
                            Log.d(TAG,"onUploadProgress" + uploadedSize * 100 / totalSize);
                            progress = uploadedSize * 100 / totalSize;
                            handler.sendEmptyMessage(0);
                        }
                        @Override
                        public void onSTSTokenExpried() {
                            Log.d(TAG,"onSTSTokenExpried");
                            // This callback is fired when the STS token expires. If the file is being uploaded when the STS token expires, resumable upload is performed after the STS token is updated.
                            vodsVideoUploadClient.refreshSTSToken(accessKeyId,accessKeySecret,securityToken,expriedTime);
                        }
                        @Override
                        public void onUploadRetry(String code, String message) {
                            // This callback is fired when the system retries the upload.
                            Log.d(TAG,"onUploadRetry" + "code" + code + "message" + message);
                        }
                        @Override
                        public void onUploadRetryResume() {
                            // This callback is fired when the upload retry succeeds and the system resumes the upload. You are notified of the successful retry.
                            Log.d(TAG,"onUploadRetryResume");
                        }
                    });

Upload management

You can call the following methods to control the upload in the upload instance VODSVideoUploadClient:

  • Pause the upload.

    // This method must be set in pairs with the vodsVideoUploadClient.resume() method.
    vodsVideoUploadClient.pause();
  • Resume the upload.

    // This method must be set in pairs with the vodsVideoUploadClient.pause() method.
    vodsVideoUploadClient.resume();
  • Cancel the upload.

    // If you cancel an upload, the upload process ends and you cannot call the vodsVideoUploadClient.resume() method to resume the upload.
    vodsVideoUploadClient.cancel();

Handle callbacks

  • onUploadProgress

    The onUploadProgress callback is fired each time a part is uploaded. The callback parameters include uploadedSize and totalSize. The value of the uploadedSize parameter indicates the size of each uploaded part. The value of the totalSize parameter indicates the total size of the file that you upload.

  • onUploadSucceed

    The onUploadSucceed callback is fired when the upload succeeds. The callback parameters include videoId and imageUrl.

    Note
    • After the video is uploaded, the videoId parameter is returned. The value of the videoId parameter indicates the video ID. Then, you can integrate ApsaraVideo Player and play the video based on vid and playauth. For more information, see Overview.

    • After an image is uploaded, the imageUrl parameter is returned. The value of the imageUrl parameter indicates the image URL. If URL signing is enabled, the image URL expires after a specific period of time. For more information, see Configure URL signing.

  • onUploadFailed

    The onUploadFailed callback is fired when the upload fails. You can view the cause of the failure based on the code and message callback parameters. In addition, a prompt is displayed on the web page. For more information about error codes, see Error codes and Handle exceptions.

  • onSTSTokenExpired

    The onSTSTokenExpried callback is fired when the STS token expires. You can send a request to the AppServer to obtain a new STS token and call the following method to resume the upload:

    refreshSTSToken(accessKeyId,accessKeySecret,securityToken,expriedTime);
  • uploadRetry

    When the upload times out, the uploadRetry callback is fired and the system automatically retries to upload the file. You can receive a prompt on the web page or call the cancel method to cancel the upload. In addition, you can set the maxRetryCount parameter to specify the maximum number of retries. If the retry result indicates that the upload can be resumed, the uploadRetryResume callback is fired and the upload is resumed.