All Products
Search
Document Center

Use the upload URL and credential

Last Updated: Jul 11, 2019

[TOC]

Overview

Background

The Client upload topic in the Developer Guide has described two authorization methods to upload media files from a client to ApsaraVideo for VOD:

  • Use the upload URL and credential
  • Use STS

This topic describes the process of using the upload URL and credential to upload media files to ApsaraVideo for VOD. We recommend that you use the upload URL and credential to upload media files. This method is more advantageous over STS. For more information, see Comparison between upload (playback) credentials and STS.

Introduction

Upload URLs and credentials are issued by ApsaraVideo for VOD for authorizing users to upload media files to the bucket allocated by ApsaraVideo for VOD. For more information, see Upload URL and credential.

Process

Client upload process.png

Implementation

  1. Register an Alibaba Cloud account, complete verification, and activate ApsaraVideo for VOD. Obtain an AccessKey to access ApsaraVideo for VOD. We recommend that you use the AccessKey of a RAM user to access ApsaraVideo for VOD. Ensure that you have granted the RAM user the AliyunVODFullAccess permission to access ApsaraVideo for VOD.
  2. Deploy the authorization service on the AppServer and obtain the upload URL and credential. We recommend that you use a server SDK to obtain the upload URL and credential.
  3. Before uploading media files to ApsaraVideo for VOD on the client, obtain the upload URL and credential from the AppServer.
  4. Add a local file on the client, set the upload URL and credential, and start the upload.

Set the upload URL and credential

You need to obtain the upload URL and a new upload credential to upload each file. We recommend that you obtain the upload URL and credential from the AppServer in the onUploadStarted callback and set them in the upload instance. The following section provides the sample code for different platforms.

Sample code for iOS

// Creates a VODUploadClient object.
self.uploader = [VODUploadClient new];
// Creates a weak reference to self.
__weak typeof(self) weakSelf = self;
// Sets callbacks.
OnUploadFinishedListener FinishCallbackFunc = ^(UploadFileInfo* fileInfo, VodUploadResult* result){
    NSLog(@"upload finished callback videoid:%@, imageurl:%@", result.videoId, result.imageUrl);
};
OnUploadFailedListener FailedCallbackFunc = ^(UploadFileInfo* fileInfo, NSString *code, NSString* message){
    NSLog(@"upload failed callback code = %@, error message = %@", code, message);
};
OnUploadProgressListener ProgressCallbackFunc = ^(UploadFileInfo* fileInfo, long uploadedSize, long totalSize) {
    NSLog(@"upload progress callback uploadedSize : %li, totalSize : %li", uploadedSize, totalSize);
};
OnUploadTokenExpiredListener TokenExpiredCallbackFunc = ^{
    NSLog(@"upload token expired callback.");
    // Sets a new upload credential to resume the upload upon credential expiration.
};
OnUploadRertyListener RetryCallbackFunc = ^{
    NSLog(@"upload retry begin callback.") ;
};
OnUploadRertyResumeListener RetryResumeCallbackFunc = ^{
    NSLog(@"upload retry end callback.") ;
};
OnUploadStartedListener UploadStartedCallbackFunc = ^(UploadFileInfo* fileInfo) {
    NSLog(@"upload upload started callback.") ;
    // Sets the upload URL and credential.
    [weakSelf.uploader setUploadAuthAndAddress:fileInfo uploadAuth:`upload auth` uploadAddress:`upload address`];
};

VODUploadListener *listener = [[VODUploadListener alloc] init];
listener.finish = FinishCallbackFunc;
listener.failure = FailedCallbackFunc;
listener.progress = ProgressCallbackFunc;
listener.expire = TokenExpiredCallbackFunc;
listener.retry = RetryCallbackFunc;
listener.retryResume = RetryResumeCallbackFunc;
listener.started = UploadStartedCallbackFunc;
// Initializes the upload instance with the upload URL and credential.
[self.uploader init:listener];

Sample code for Android

VODUploadClient uploader = new VODUploadClientImpl(getApplicationContext());
VODUploadCallback callback = new VODUploadCallback() {
    public void onUploadSucceed(UploadFileInfo info) {
        // This callback is fired when the upload succeeds.
    }

    public void onUploadFailed(UploadFileInfo info, String code, String message) {
          // This callback is fired when the upload fails.
    }

    public void onUploadProgress(UploadFileInfo info, long uploadedSize, long totalSize) {
        // This callback is fired when the default or custom upload progress is reached.
    }

    public void onUploadTokenExpired() {
        // This callback is fired when the upload credential expires. The operation for updating the credential needs to be called.
    }

    public void onUploadRetry(String code, String message) {
        // This callback is fired when the system retries the upload upon a network exception.
    }

    public void onUploadRetryResume() {

    }

    public void onUploadStarted(UploadFileInfo uploadFileInfo) {
        OSSLog.logError("onUploadStarted ------------- ");
        // This callback is fired when the upload starts. We recommend that you obtain the upload URL and credential from the AppServer in this callback.
        // Sets the uploadAuth and uploadAddress parameters.                
        uploader.setUploadAuthAndAddress(uploadFileInfo, uploadAuth, uploadAddress);
    }
};

uploader.init(callback);

Sample code for HTML5 and JavaScript

var uploader = new AliyunUpload.Vod({
    partSize: 1048576,// The size of each part in multipart upload. The default value is 1 MB. The value cannot be less than 100 KB.
       parallel: 5,// The maximum number of parts that can be uploaded in parallel. The default value is 5.
    retryCount: 3,// The maximum number of attempts to retry the upload upon a network exception. The default value is 3.
    retryDuration: 2,// The interval for retrying the upload upon a network exception, in seconds. The default value is 2.
    'onUploadstarted': function (uploadInfo) {
          uploader.setUploadAuthAndAddress(
              uploadInfo, 
            uploadAuth, 
            uploadAddress,
            videoId);
    }
    …… // Sets other callbacks.
});

Further reading

For more information about SDK integration, file list management, and callbacks upon a successful upload and credential expiration, see the documentation of upload SDKs for different platforms: