The following describes how to quickly build an audio and video file upload service based on the OSS service and MPS’s SDK upload.


Uploading audio and video files using MPS’s SDK offers the following advantages:
  • Adds file list management.

  • Adds STS Token timeout update function.

  • Auto-retry function when network jitter occurs in the process of uploading.

  • File resume breakpoint function.

  • Workflow that automatically triggers the MPS service.

  • Configures media titles, tags, descriptions, categories, cover URLs, and more.

    • Restrictions on resuming HTTP: does not allow cross-lifecycle. JS side page can not be refreshed, closed, and Android/iOS can not close the APP and mobile phone.
    • The same local file can only be uploaded once.

Server creation

Consider mobile AK security issues, choose STS to upload files. To learn how using STS increases the security of the upload, see RAM and STS User Guide.

STS Activation Procedure

  1. Activate the OSS service, create a bucket, and log on to the OSS console.
  2. Find the basic configuration area on the OSS overview page and click the security token.

  3. Go to the Security Token Shortcut Configuration page.

  4. Authorize automatically and save parameters in the text boxes. Click Save AK Info to close the dialog and complete STS activation.

Build an application server

Configuration of app server sample code

This document provides three development example programs available for download in three languages.
The download for each language pack contains a configuration file: config.json:
  1. {“AccessKeyID :””,“AccessKeySecret :””,“RoleArn : “”,“TokenExpireTime : 900”,“PolicyFile”: policy/all_policy.txt”}
  • AccessKeyID: Set the parameter value marked 1 in the above diagram.

  • AccessKeySecret: Set the parameter value marked 2 in the above diagram.

  • RoleArn: Set the parameter value marked 3 in the above diagram.

  • TokenExpireTime: Indicates the expiration time of the token obtained by the Android/iOS app. Note: The minimum value is 900s. The default value can be retained.

  • PolicyFile: Fill in the list of rights to the Token file, the default value can not be modified.

This document has provided three token files defining the most common permissions in the policy directory. They are:
  • all_policy.txt: Specifies that the token has the authority to create or delete a bucket, to upload or download a file, and to delete a file under this account.

  • bucket_read_policy.txt: Specifies that the token has read access to the specified bucket under this account.

  • bucket_read_write_policy.txt: Specifies a token that grants read and write permissions for the specified bucket for this account.

If you want to create a token to grant read and write permissions for the specified bucket, replace $BUCKET_NAME in the bucket_read_policy.txt and bucket_read_write_policy.txt files with the name of the desired bucket.

  • Return format resolution:
    1. {“status”:200,“AccessKeyId”:”test”,“AccessKeySecret”:”test”,“Security”:”CAES+wMIARKAAZhjH0EUOIhJMQBMjRywXq7MQ/cjLYg80Aho1ek0Jm63XMhr9Oc5s3qaPer8p1YaX1NTDiCFZWFkvlHf1pQhuxfKBc+mRR9KAbHUefqH+rdjZqjTF7p2m1wJXP8S6k+G2MpHrUe6TYBkJ43GhhTVFMuM3BZajY3VjZWOXBIODRIR1FKZjIiEjMzMzE0MjY0NzM5MTE4NjkxMSoLY2xpZGSSDgSDGAGESGTETqOio6c2RrLWRlbW8vKgoUYWNzOm9zczoqOio6c2RrLWRlbW9KEDExNDg5MzAxMDcyNDY4MThSBTI2ODQyWg9Bc3N1bWVkUm9sZVVzZXJgAGoSMzMzMTQyNjQ3MzkxMTg2OTExcglzZGstZGVtbzI=”,“Expiration”:”2015-12-12T07:49:09Z”,}
    Note (the four variables shown below comprise a token) :
    • status indicates the result that the app retrieves the token. The app returns a 200 status code for successful retrieval of the token.

    • AccessKeyId indicates the AccessKeyId the Android/iOS app obtains when initializing the OSS client.

    • AccessKeySecret indicates the AccessKeySecret the Android/iOS app obtains when initializing the OSS client.

    • SecurityToken indicates the token the Android/iOS app initializes.

    • Expiration indicates the time when the token expires. The Android SDK will automatically determine the validity of the token and retrieve a new one as needed.

  • Examples of how to run code:
    • For JAVA (based on Java 1.7), after downloading and unzipping a pack,

      run this command: java -jar oss-token-server.jar (port). If you run java –jar oss-token-server.jar without specifying a port, the program listens to Port 7080. To change the listening port to 9000, run java –jar app-token-server.jar 9000. Specify the port number as needed.

    • For PHP, after you download and decompress the package, modify the config.json file and run php sts.php directly to generate a token. Then set up the app server at the specified address.

Use the MPS client SDK

  • Client Sample Code
    This document provides three development example programs available for download in three languages.
  • SDK core code

    JS side

    Before using the JS SDK, first open CORS Access to the OSS Bucket where you want to upload the video. Download JS Demo, open in a browser, the parameters on the page configuration are:
    • Configure the “HTTP Address” as the application server address configured above, for example,
    • Configure user Bucket.
    • Configure Bucket endpoint.
    • Click to select the file, select the file to be uploaded.
    • Click the Start Upload button.
    1. // Initialize the clientvar uploader = new VODUpload({// Start upload‘onUploadstarted’: function (uploadInfo) {;},//File uploaded successfully‘onUploadSucceed’: function (uploadInfo) {console.log(“Uploaded successfully”);},//File upload failed‘onUploadFailed’: function (uploadInfo, code, message) {console.log(“File upload failed”);},// File upload progress, in bytes‘onUploadProgress’: function (uploadInfo, totalSize, uploadedSize) {console.log(“File upload progress,”);},//Security token timed out‘onUploadTokenExpired’: function (uploadInfo) {console.log(“Token timeout”);}});// Get STS Informationresult = httpGet(httpServer);stsToken = JSON.parse(result);uploader.init(stsToken.AccessKeyId, stsToken.AccessKeySecret, stsToken.SecurityToken, stsToken.Expiration);// Add Fileuploader.addFile([i], endpoint, bucket, object, userData);// Start uploadinguploader.startUpload();

    Android end

    Make sure Android has added the following permissions:
    1. <uses-permission android:name=”android.permission.INTERNET”></uses-permission><uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE”></uses-permission><uses-permission android:name=”android.permission.ACCESS_WIFI_STATE”></uses-permission><uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”></uses-permission>
    Download Android Demo, make the following changes:
    • Modify MainActivity inside serverUrl for the application server configuration address, such as
    • Configure user Bucket.
    • Configure the endpoint corresponding to the user Bucket.
    • Run Demo, click Add File.
    • Click Upload to check whether the file has been uploaded successfully under the uploadtest / directory of the OSS Bucket.
    Main code:
    1. VODUploadClient uploader = new VODUploadClientImpl(getApplicationContext());VODUploadCallback callback = new VODUploadCallback() {@Overridepublic void onUploadSucceed(UploadFileInfo info) {;}@Overridepublic void onUploadFailed(UploadFileInfo info, String code, String message) {;}@Overridepublic void onUploadProgress(UploadFileInfo info, long uploadedSize, long totalSize) {;}@Overridepublic void onUploadTokenExpired(UploadFileInfo info) {// Get and update STS token.uploader.resumeWithToken(“”, “”, “”, “”);}@Overridepublic void onUploadRetry(UploadFileInfo info, String code, String message) {;}@Overridepublic void onUploadRetryResume(UploadFileInfo info) {;}@Overridepublic boolean onUploadStarted(UploadFileInfo uploadFileInfo) {;}};// Get STS token and initializeuploader.init(“”, “”, “”, “”, callback);// Add Fileuploader.addFile(“”, “”, “”, “”);// Start uploadinguploader.start();

    IOS end

    Download iOS Demo, make the following changes:
    • Modify the serverUrl in VODUploadDemo.m to configure the address for the application server, such as
    • Configure user Bucket.
    • Configure the endpoint corresponding to the user Bucket.
    • Run Demo, click Add File.
    • Click Upload to check whether the file has been uploaded successfully under the uploadtest / directory of the OSS Bucket.
    Main code:
    1. // Callback InitializationOnUploadStartedListener testUploadStartedCallbackFunc = ^(UploadFileInfo fileInfo) {;};OnUploadSucceedListener testSuccessCallbackFunc = ^(NSString filePath){;};OnUploadFailedListener testFailedCallbackFunc = ^(NSString filePath, NSString code, NSString message){;};OnUploadProgressListener testProgressCallbackFunc = ^(NSString filePath, long uploadedSize, long totalSize) {;};OnUploadTokenExpiredListener testTokenExpiredCallbackFunc = ^{// Get and update STS token[uploader resumeWithToken:accessKeySecret:secretToken:expireTime:]};OnUploadRertyListener testUploadRertyListener = ^{;};OnUploadRertyResumeListener testUploadRertyResumeListener = ^{;};VODUploadListener listener;listener = [[VODUploadListener alloc] init];listener.started = testUploadStartedCallbackFunc;listener.success = testSuccessCallbackFunc;listener.failure = testFailedCallbackFunc;listener.progress = testProgressCallbackFunc;listener.expire = testTokenExpiredCallbackFunc;listener.retry = testUploadRertyListener;listener.retryResume = testUploadRertyResumeListener;// Get Token// Upload client InitializationVODUploadClient uploader;[uploader init:accessKeySecret:secretToken:expireTime:listener:listener];// Add File[uploader addFile:endpoint:bucket:object:];// Start uploading[uploader start];