Help Center> Video On Demand> API Reference> Application Examples> Example 1: Uploading a Media File Less Than 20 MB
Updated on 2022-10-24 GMT+08:00

Example 1: Uploading a Media File Less Than 20 MB

Scenario

If you need to process (such as publish and transcode) your local media files in VOD, then you can call VOD APIs and OBS APIs to upload them to OBS buckets owned by VOD.

This section describes how to upload a video file less than 20 MB by calling APIs.

Prerequisites

Overall Process

Figure 1 Media upload process
  1. Call the API for uploading media files to VOD to create a media file.
  2. Upload a local media file using PUT.
  3. Call the API for confirming the media file upload to confirm the media file upload.

Procedure

  1. Obtain a user token and use it to authenticate the calling of VOD APIs.

    For details, see Making an API Request. CN North-Beijing4 is used as an example. If you need to call a VOD API in another region, replace the endpoint with the IAM endpoint of the corresponding region.

    POST https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens
    Content-Type: application/json
    
    {
      "auth": {
        "identity": {
          "methods": [
            "password"
          ],
          "password": {
            "user": {
              "name": "username", 
              "password": "password", 
              "domain": {
                "name": "domainname"   
              }
            }
          }
        },
        "scope": {
          "project": {
             "name": "projectname"
          }
        }
      }
    }
    As shown in Figure 2, information in the red box indicates the user token.
    Figure 2 Obtaining the user token

  2. Call the API for uploading media files to VOD to create a media file. Add the X-Auth-Token field to the request header. The value of Value is obtained in 1.

    POST https://vod.cn-north-4.myhuaweicloud.com/v1.0/{project_id}/asset
    
    {
    	"title": "test",
    	"description": "test",
    	"category_id": 87748,
    	"video_name": "test.mp4",
    	"video_type": "MP4",
    	"cover_type": "PNG",
    	"tags":"mytags,test",
    	"auto_publish": 0,    
    	"template_group_name":"test",
    	"subtitles":[      
    		{	
    			"id":1,
    			"language":"CN", 
    			"type": "SRT", 
    			"md5": "SqcyFjJZoDZaP8oKIY6rgQ==",    
    			"description":"AAAAA" ,
    			"name":"cc.srt"
    		}  
    	],
    	"thumbnail":{
    		"type":"time",
    		"cover_position":1
    	
    	},
    	"review":{
    		"interval":10,
    		"politics":-1,
    		"terrorism":-1,
    		"porn":-1
    	}	
    }
    vod.cn-north-4.myhuaweicloud.com is the endpoint of VOD. You can obtain the endpoint from Endpoints.

    The region where an IAM API is called must be the same as the region where a VOD API is called. For example, the user token obtained when you call an IAM API in the CN North-Beijing4 region can only be used to authenticate the requests for calling VOD APIs in the CN North-Beijing4 region.

  3. Obtain the values of video_upload_url, cover_upload_url, and subtitle_upload_urls from the response parameters.

    {
      "asset_id": "3f40a2c7c60454f5f84381e0313ca230",
      "video_upload_url": "https://vod-bucket-81.obs.cn-north-4.myhuaweicloud.com:443/474bcff2992f4be5b883a2fb9cec7343/3f40a2c7c60454f5f84381e0313ca230/cdeda86cd1b7b3dd760a3ff28a5ee497.mp4?AWSAccessKeyId=BG923RWHL4HFXOGKCVAL&Expires=1560420274&Signature=9f%2BZcdD6SwjIU5ARHYiP6YY1Lyw%3D",
      "cover_upload_url": "https://vod-bucket-81.obs.cn-north-4.myhuaweicloud.com:443/474bcff2992f4be5b883a2fb9cec7343/3f40a2c7c60454f5f84381e0313ca230/cover/Cover0.png?AWSAccessKeyId=BG923RWHL4HFXOGKCVAL&Expires=1560420274&Signature=4Aa88NK%2By%2By1Xo0RvLpOvuaFCoE%3D",
      "subtitle_upload_urls": [
        "https://vod-bucket-81.obs.cn-north-4.myhuaweicloud.com:443/474bcff2992f4be5b883a2fb9cec7343/3f40a2c7c60454f5f84381e0313ca230/subtitle/1.srt?AWSAccessKeyId=BG923RWHL4HFXOGKCVAL&Expires=1560420274&Signature=l0UclE9yfaVrxkl0kaNnr%2BemG98%3D"
      ],
      "target": {
        "bucket": "vod-bucket-81",
        "location": "cn-north-4",
        "object": "474bcff2992f4be5b883a2fb9cec7343/3f40a2c7c60454f5f84381e0313ca230/cdeda86cd1b7b3dd760a3ff28a5ee497.mp4"
      }
    }

  4. Send PUT requests to upload video files, thumbnails, and subtitle files to video_upload_url, cover_upload_url, and subtitle_upload_urls.

    Set Content-Type based on the type of the file to be uploaded.
    • Video file: video/video format, for example, video/mp4. For details about how to fill in the request header of a video file format, see Table 1.
    • Image file: image/image format, for example, image/png.
    • Subtitle file: application/octet-stream
    Table 1 Filling in request headers of different video formats

    File Name Extension

    Content-Type

    MP4

    video/mp4

    MOV

    video/quicktime

    MXF

    application/mxf

    TS

    video/mp2t

    MPG

    video/mpeg

    FLV

    video/flv

    WMV

    video/x-ms-wmv

    AVI

    video/x-msvideo

    M4V

    video/m4v

    F4V

    application/f4v

    MPEG

    video/mpeg

    M3U8

    application/octet-stream

    _3GP/3GP

    video/3gpp

    ASF

    video/x-ms-asf

    MKV

    video/x-matroska

    WEBM

    video/webm

    MPD

    video/dash

  5. Call the API for confirming the media file upload to confirm the media file upload.

    POST https://vod.cn-north-4.myhuaweicloud.com/v1.0/{project_id}/asset/status/uploaded
    
    {
    	 "asset_id": "0f4d3f1f32ec353d8866f2d84a036124",
    	 "status":"CREATED"
    }

After a media file has been uploaded, you can view details about the media file on the VOD console.