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.
A user deploys an authorization service on an application server, such as ApsaraVideo VOD SDK, to obtain temporary STS tokens.
A client sends a request to the application server to request an STS token.
The application server sends a request to STS to request the STS token.
STS returns the token.
The application server returns the STS token to the client.
The client uses the STS token to initialize an upload instance.
The client constructs upload parameters to send an upload request.
OSS returns the upload result.
NoteYou can also set callbacks in advance to receive notifications about upload events.
Upload a file
Obtain an STS token. For more information, see Obtain an STS token.
Declare a
VODSVideoUploadClient
object. The object cannot be a local variable.// Initialize the VODSVideoUploadClient object. VODSVideoUploadClient vodsVideoUploadClient = new VODSVideoUploadClientImpl(this.getApplicationContext()); vodsVideoUploadClient.init();
Construct upload parameters.
Set callbacks and start the upload.
You must set the
VODSVideoUploadCallback
callback before you start the upload.
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 includeuploadedSize
andtotalSize
. 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 includevideoId
andimageUrl
.NoteAfter 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 thecode
andmessage
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 thecancel
method to cancel the upload. In addition, you can set themaxRetryCount
parameter to specify the maximum number of retries. If the retry result indicates that the upload can be resumed, theuploadRetryResume
callback is fired and the upload is resumed.