Help Center> Video On Demand> API Reference> Application Examples> Example 2: Uploading a Media File Greater Than 20 MB by Part
Updated on 2024-03-01 GMT+08:00

Example 2: Uploading a Media File Greater Than 20 MB by Part

Scenario

Example 1: Uploading a Media File Less Than 20 MB has described how to upload a media file less than 20 MB to VOD. If you need to upload a media file greater than 20 MB, you can call VOD APIs to upload the file by part.

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

Prerequisites

  • You have determined the region where the VOD service for uploading media files is located. See Endpoints.
  • You have obtained the ID of the project. For details, see Obtaining a Project ID.
  • The video file to be uploaded has been split into binary streams. The size of each part cannot exceed 20 MB.

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 a user token

  2. Call the API for uploading media files to VOD to create a media file.

    1. Select the POST request method and enter the URI.
      https://vod.cn-north-4.myhuaweicloud.com/v1.0/{project_id}/asset
    2. In the request header, add X-Auth-Token and set its value to the token obtained in 1.
    3. Specify the following parameters in the request body:
      {
      	"title": "testVideo",
      	"description": "test",
      	"category_id": 87748,
      	"video_name": "test.mp4",
      	"video_type": "MP4",
      	"auto_publish": 0    
      }

      Only some media file parameters are configured in the example. You can configure all parameters by referring to the API for uploading media files to VOD.

    4. If the request is successful, the media file ID, video upload URL, and information about the OBS bucket that stores the video file are returned.
      {
          "asset_id": "fea62a2845e43c37b4cbfb017c0d0821",
          "video_upload_url": "https://vod-bucket-65-cn-north-4.obs.cn-north-4.myhuaweicloud.com:443/05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4?AWSAccessKeyId=MZH0LUL329N1SSXNB3S4&Expires=1596700132&Signature=GiHTcO2avO9B84sH8A5wL2GieCI%3D",
          "cover_upload_url": null,
          "subtitle_upload_urls": [],
          "target": {
              "bucket": "vod-bucket-65-cn-north-4",
              "location": "cn-north-4",
              "object": "05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4"
          }
      }

  3. Call the API for obtaining authorization for multi-part upload to obtain the authorization for initializing an upload task from OBS.

    1. Select the GET request method and enter the request URI.
      https://vod.cn-north-4.myhuaweicloud.com/v1.1/{project_id}/asset/authority?http_verb=POST&content_type=video/mp4&bucket=vod-bucket-65-cn-north-4&object_key=05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4
      • http_verb: HTTP method for calling the OBS API for multi-part upload. For details about the suitable HTTP method, see OBS API Reference.
      • content_type: HTTP Content-type of a certain file type. You need to configure this parameter based on the type of the uploaded file.
        • 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.
        • Audio file: audio/audio format, for example, audio/mp3. For details about how to fill in the request header of a media file format, see Table 2.
        • Image file: image/image format, for example, image/png.
        • Subtitle file: application/octet-stream
      • bucket: OBS bucket for storing video files. It corresponds to the bucket field returned in 2.d.
      • object_key: name of the object in the OBS bucket. It corresponds to the object field returned in 2.d.
      Table 1 Parameters in the request header of a video file

      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

      Table 2 Parameters in the request header of an audio file

      File Name Extension

      Content-Type

      MP3

      audio/mp3

      WMA

      audio/wma

      APE

      audio/ape

      FLAC

      audio/flac

      AAC

      audio/aac

      AC3

      audio/ac3

      MMF

      audio/mmf

      AMR

      audio/amr

      M4A

      audio/m4a

      M4R

      audio/m4r

      OGG

      audio/ogg

      WAV

      audio/wav

      WV

      audio/wv

      MP2

      audio/mp2

    2. In the request header, add X-Auth-Token and set its value to the token obtained in 1.
    3. If the request is successful, information about the authorization is returned.
      {
          "sign_str": "https://vod-bucket-65-cn-north-4.obs.cn-north-4.myhuaweicloud.com:443/05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4?uploads&AWSAccessKeyId=MZH0LUL329N1SSXNB3S4&Expires=1596706429&Signature=5D15iJUcTkoWLpE2vn54FQZskXA%3D"
      }

  4. Call the OBS API for initializing a multi-part upload task to initialize the multi-part upload task.

    1. Select the POST request method and enter the authorized URI returned in 3.c.
      https://vod-bucket-65-cn-north-4.obs.cn-north-4.myhuaweicloud.com:443/05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4?uploads&AWSAccessKeyId=MZH0LUL329N1SSXNB3S4&Expires=1596706429&Signature=5D15iJUcTkoWLpE2vn54FQZskXA%3D
    2. Add Content-Type to the request header and configure this parameter based on the format of the uploaded video. For details, see 3.
    3. If the request is successful, the initialization information is returned.
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <InitiateMultipartUploadResult xmlns="http://obs.cn-north-4.myhuaweicloud.com/doc/2015-06-30/">
          <Bucket>vod-bucket-65-cn-north-4</Bucket>
          <Key>05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4</Key>
          <UploadId>00000173C2ED862344C835374DFE33C8</UploadId>
      </InitiateMultipartUploadResult>

  5. Call the API for obtaining authorization for multi-part upload to obtain the authorization for initializing a multi-part upload task.

    1. Select the GET request method and enter the request URI.
      https://vod.cn-north-4.myhuaweicloud.com/v1.1/{project_id}/asset/authority?http_verb=PUT&content_type=video/mp4&bucket=vod-bucket-65-cn-north-4&object_key=05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4&content_md5=hHZXtgjYL2fmLpu%2byQ%2bXgg%3d%3d&upload_id=00000173C2ED862344C835374DFE33C8&part_number=1
      • http_verb: The HTTP method for multi-part upload is PUT.
      • content_type: HTTP Content-type of a certain file type. You need to configure this parameter based on the type of the uploaded file. For details, see 3.
      • bucket: OBS bucket for storing video files. It corresponds to the bucket field returned in 2.d.
      • object_key: name of the object in the OBS bucket. It corresponds to the object field returned in 2.d.
      • content_md5: MD5 value of each video part. This parameter is mandatory. For details about how to generate an MD5 value, see Generating an MD5 Value. Note: If there are special characters in the request, escape them.
      • upload_id: ID of the upload task, which is the UploadId field returned in 4.c.
      • part_number: number of an uploaded part
    2. In the request header, add X-Auth-Token and set its value to the token obtained in 1.
    3. If the request is successful, information about the authorization for multi-part upload is returned.
      {
          "sign_str": "https://vod-bucket-65-cn-north-4.obs.cn-north-4.myhuaweicloud.com:443/05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4?AWSAccessKeyId=MZH0LUL329N1SSXNB3S4&Expires=1596708691&partNumber=1&uploadId=00000173C2ED862344C835374DFE33C8&Signature=cjw3CmUFeNBFAuLWBTsPRp9NfsQ%3D"
      }

  6. Call the multi-part upload API of OBS to upload the first part of the video file.

    1. Select the PUT request method and enter the authorized URI returned in 5.c.
      https://vod-bucket-65-cn-north-4.obs.cn-north-4.myhuaweicloud.com:443/05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4?AWSAccessKeyId=MZH0LUL329N1SSXNB3S4&Expires=1596708691&partNumber=1&uploadId=00000173C2ED862344C835374DFE33C8&Signature=cjw3CmUFeNBFAuLWBTsPRp9NfsQ%3D
    2. Add Content-Type to the request header and set the parameter to application/octet-stream. If content_md5 has been configured in 5.a, Content-MD5 must be added to the request header and set to the MD5 value of the corresponding part (escaping is not required). Otherwise, the upload will fail.
    3. In the request body, the first part of the video file is uploaded in binary streams.
    4. If the request is successful, the status code 200 OK is returned.

  7. Repeat 5 to 6 to upload all parts of the video file.
  8. Call the API for obtaining authorization for multi-part upload to obtain the authorization for listing uploaded parts.

    1. Select the GET request method and enter the request URI.
      https://vod.cn-north-4.myhuaweicloud.com/v1.1/{project_id}/asset/authority?http_verb=GET&bucket=vod-bucket-65-cn-north-4&object_key=05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4&upload_id=000001717E28524B44CEFF2C2CE1B06A
      • http_verb: The HTTP method for listing uploaded parts is GET.
      • bucket: OBS bucket for storing video files. It corresponds to the bucket field returned in 2.d.
      • object_key: name of the object in the OBS bucket. It corresponds to the object field returned in 2.d.
      • upload_id: ID of the upload task, which is the UploadId field returned in 4.c.
    2. In the request header, add X-Auth-Token and set its value to the token obtained in 1.
    3. If the request is successful, information about the authorization for listing the uploaded parts is returned.
      {
          "sign_str": "https://vod-bucket-65-cn-north-4.obs.cn-north-4.myhuaweicloud.com:443/05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4?AWSAccessKeyId=MZH0LUL329N1SSXNB3S4&Expires=1596710054&uploadId=000001717E28524B44CEFF2C2CE1B06A&Signature=G5EOUr488cyPIretp8lgQZmpPw0%3D"
      }

  9. Call the OBS API for listing uploaded parts to obtain information about all uploaded parts of the video file.

    Note: The Content-Type attribute in the request header is set to null or is not carried.

    1. Select the GET request method and enter the authorized URI returned in 8.c.
      https://vod-bucket-65-cn-north-4.obs.cn-north-4.myhuaweicloud.com:443/05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4?AWSAccessKeyId=MZH0LUL329N1SSXNB3S4&Expires=1596710054&uploadId=000001717E28524B44CEFF2C2CE1B06A&Signature=G5EOUr488cyPIretp8lgQZmpPw0%3D
    2. If the request is successful, information about the uploaded parts is returned.
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <ListPartsResult xmlns="http://obs.cn-north-4.myhuaweicloud.com/doc/2015-06-30/">
          <Bucket>vod-bucket-65-cn-north-4</Bucket>
          <Key>05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4</Key>
          <UploadId>00000173C2ED862344C835374DFE33C8</UploadId>
          <Initiator>
              <ID>d9235b9f3cf549499924f6de095241af:a96ecebcb3994e34b074e48f3dfc8237</ID>
              <DisplayName>xxx</DisplayName>
          </Initiator>
          <Owner>
              <ID>d9235b9f3cf549499924f6de095241af</ID>
              <DisplayName>xxx</DisplayName>
          </Owner>
          <StorageClass>STANDARD</StorageClass>
          <PartNumberMarker>0</PartNumberMarker>
          <NextPartNumberMarker>4</NextPartNumberMarker>
          <MaxParts>1000</MaxParts>
          <IsTruncated>false</IsTruncated>
          <Part>
              <PartNumber>1</PartNumber>
              <LastModified>2020-08-06T09:05:10.192Z</LastModified>
              <ETag>"847657b608d82f67e62e9bbec90f9782"</ETag>
              <Size>10000000</Size>
          </Part>
          <Part>
              <PartNumber>2</PartNumber>
              <LastModified>2020-08-06T08:02:02.821Z</LastModified>
              <ETag>"9a6a36ed9086a3a2fea130220e1e809c"</ETag>
              <Size>10000000</Size>
          </Part>
          <Part>
              <PartNumber>3</PartNumber>
              <LastModified>2020-08-06T08:04:19.711Z</LastModified>
              <ETag>"3a3afe552832bee0faf081c1e720067e"</ETag>
              <Size>10000000</Size>
          </Part>
          <Part>
              <PartNumber>4</PartNumber>
              <LastModified>2020-08-06T07:23:17.160Z</LastModified>
              <ETag>"6335af859c20ccef26b27ea691e7ecf7"</ETag>
              <Size>7472445</Size>
          </Part>
      </ListPartsResult>

  10. Call the API for obtaining authorization for multi-part upload to obtain the authorization for merging uploaded parts.

    1. Select the GET request method and enter the request URI.
      https://vod.cn-north-4.myhuaweicloud.com/v1.1/{project_id}/asset/authority?http_verb=POST&bucket=vod-bucket-65-cn-north-4&object_key=05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4&upload_id=00000173C2ED862344C835374DFE33C8
      • http_verb: The HTTP method for merging parts is POST.
      • bucket: OBS bucket for storing video files. It corresponds to the bucket field returned in 2.d.
      • object_key: name of the object in the OBS bucket. It corresponds to the object field returned in 2.d.
      • upload_id: ID of the upload task, which is the UploadId field returned in 4.c.
    2. In the request header, add X-Auth-Token and set its value to the token obtained in 1.
    3. If the request is successful, information about the authorization for merging uploaded parts is returned.
      {
          "sign_str": "https://vod-bucket-65-cn-north-4.obs.cn-north-4.myhuaweicloud.com:443/05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4?AWSAccessKeyId=MZH0LUL329N1SSXNB3S4&Expires=1596709340&uploadId=000001717E28524B44CEFF2C2CE1B06A&Signature=Pa6IaMbH1Ofa0Vi%2BCbkdgmWzm70%3D"
      }

  11. Call the OBS API for merging parts to merge the uploaded parts into a video file.

    1. Select the POST request method and enter the authorized URI returned in 10.c.
      https://vod-bucket-65-cn-north-4.obs.cn-north-4.myhuaweicloud.com:443/05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4?AWSAccessKeyId=MZH0LUL329N1SSXNB3S4&Expires=1596708691&partNumber=1&uploadId=00000173C2ED862344C835374DFE33C8&Signature=cjw3CmUFeNBFAuLWBTsPRp9NfsQ%3D
    2. Add Content-Type to the request header and set the parameter to application/xml.
    3. Specify the following parameters in the request body:
      <CompleteMultipartUpload> 
           <Part>
              <PartNumber>1</PartNumber>
              <ETag>"847657b608d82f67e62e9bbec90f9782"</ETag>
          </Part>
          <Part>
              <PartNumber>2</PartNumber>
              <ETag>"9a6a36ed9086a3a2fea130220e1e809c"</ETag>
          </Part>
          <Part>
              <PartNumber>3</PartNumber>
              <ETag>"3a3afe552832bee0faf081c1e720067e"</ETag>
          </Part>
          <Part>
              <PartNumber>4</PartNumber>
              <ETag>"6335af859c20ccef26b27ea691e7ecf7"</ETag>
          </Part>
      </CompleteMultipartUpload>
      • PartNumber: number of an uploaded part
      • ETag: ETag value returned after a video part has been uploaded. The returned information in 9.b contains the ETag value of each part.
    4. If the request is successful, information about the merged parts is returned.
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <CompleteMultipartUploadResult xmlns="http://obs.cn-north-4.myhuaweicloud.com/doc/2015-06-30/">
          <Location>05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4</Location>
          <Bucket>vod-bucket-65-cn-north-4</Bucket>
          <Key>05041fffa4002****f6dc009cc6f8f33/fea62a2845e43c37b4cbfb017c0d0821/d1f221f09f6bffefb882c8f9e167483a.mp4</Key>
          <ETag>"87f450dd18b666dfcdf902ac6b162b5a-4"</ETag>
      </CompleteMultipartUploadResult>

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

    1. Select the POST request method and enter the URI.
      https://vod.cn-north-4.myhuaweicloud.com/v1.0/{project_id}/asset/status/uploaded
    2. In the request header, add X-Auth-Token and set its value to the token obtained in 1.
    3. Specify the following parameters in the request body:
      {
         "asset_id": "fea62a2845e43c37b4cbfb017c0d0821",
         "status":"CREATED" 
      }
    4. If the request is successful, the media file ID is returned.
      {
         "asset_id": "fea62a2845e43c37b4cbfb017c0d0821" 
      }