All Products
Search
Document Center

ApsaraVideo VOD:Use the upload SDK for C/C+ to upload files

Last Updated:Jan 03, 2024

This topic describes how to use the server upload SDK for C or C++ to upload media files to ApsaraVideo VOD.

Upload process

The upload SDK for C or C++ follows the general process of ApsaraVideo VOD SDKs. For more information, see Upload process. The following content describes the basic process of using the server upload SDK for C or C++:

  1. Complete the configurations as described in the Prerequisites section.

  2. Integrate the server upload SDK for C or C++. For more information, see Integrate the server upload SDK for C or C++.

  3. Configure upload information to implement the upload logic.

Prerequisites

  • ApsaraVideo VOD is activated. For more information, see Activate ApsaraVideo VOD.

  • The system settings required for the upload, including the storage path in the specified region and the callback settings, are configured. For more information, see Manage storage buckets and Configure callbacks.

  • A RAM user is created and used to access ApsaraVideo VOD. To prevent security risks caused by the leakage of the AccessKey pair of your Alibaba Cloud account, we recommend that you create a RAM user and grant the RAM user the permissions required to access ApsaraVideo VOD. Then, you can use the AccessKey pair of the RAM user to access ApsaraVideo VOD. For more information, see Create and grant permissions to a RAM user.

  • Optional. A role is created for the RAM user and the role is granted the permissions required to access ApsaraVideo VOD if you want to access ApsaraVideo VOD by using Security Token Service (STS). For more information, see Use STS to upload videos.

    Note

    For more information about the scenarios in which STS can be used, see Comparison between credentials and STS.

Integrate the server upload SDK for C or C++

Note
  • The server upload SDK for C or C++ can be installed only in the Linux operating system.

  • In this example, the SDK V1.0.0 is used. You can use other versions based on your business requirements.

  • For more information about the directories of the server upload SDK for C or C++ and sample code, see Directories.

  • This topic describes how to install the server upload SDK for C or C++ as a non-root user and provides examples of installation commands.

The following table describes the required software packages and their usage, and installation examples.

Software package

Description

Installation by using YUM

Installation by using packages downloaded over the Internet

CMake

A third-party compiler.

  • Version: V2.6.0 or later

  • Sample code:

    sudo yum install cmake
  • Download link: CMake

  • Sample code:

    ./configure
    make
    make install

libcurl

Troubleshoots network connection issues.

  • Version: V7.29.0 or later

  • Sample code:

    sudo yum install libcurl-devel
  • Download link: libcurl

  • Sample code:

    ./configure
    make
    make install

libuuid

Generates UUIDs.

Sample code:

sudo yum install libuuid-devel

N/A

apr

N/A

Sample code:

sudo yum install apr-devel
  • Download link: apr

  • Sample code:

    ./configure
    make
    make install

apr-util

Manages memory usage and cross-platform issues.

Sample code:

sudo yum install apr-util
  • Download link: apr-util

  • Sample code:

    // Specify the with-apr parameter when you install the package. 
    ./configure --with-apr=/your/apr/install/path
    make
    make install

minixml

Parses data that is returned in the XML format.

Sample code:

sudo yum install mxml mxml-devel
  • Download link: minixml

  • Sample code:

    ./configure
    make
    make install

jsoncpp

Parses data that is returned in the JSON format.

Sample code:

sudo yum install jsoncpp-devel
  • Download link: jsoncpp

  • Sample code:

    ./configure
    make
    make install

OSS SDK

Before you use the server upload SDK for C or C++, you must download and install Object Storage Service (OSS) SDK for C or C++.

N/A

For more information about the download link and installation procedure, see Installation.

Important

If some OSS dependencies have been installed, you do not need to install them again.

Upload SDK for C or C++

N/A

Download link: Upload SDK

Sample code:

cmake .
make
make install

Update the upload SDK for C or C++

If new operations or new features of existing operations are unavailable in the current SDK, update the SDK to the latest version. For more information, see Upload SDK.

Note

You can obtain the version number and release date of the current SDK from the ChangeLog.txt file in the aliyun-c-sdk-vod directory.

Directories

/VodSDK-C_1.0.0.gz/VodSDK-C_1.0.0/aliyun-c-sdk-vod/src/upload.h

Directory

Description

CreateUploadVideoRequest

The request class for video upload. For more information about the parameters, see CreateUploadVideo.

CreateUploadImageRequest

The request class for image upload. For more information about the parameters, see CreateUploadImage.

CreateUploadAttachedMediaRequest

The request class for the upload of auxiliary media assets. For more information about the parameters, see CreateUploadAttachedMedia.

UploadOptions

The upload parameter structure. The following items describe the parameters.

  • void (*uploadProgressCallback) (int64_t, int64_t): configures a callback function for the upload progress. If you do not set this parameter, the default callback configurations are used. If you set this parameter to NULL, no callback for the upload progress is invoked.

  • ecsRegionId: specifies the region of the ECS instance where the upload script is deployed. If the ECS region is the same as the storage region of ApsaraVideo VOD, the file is automatically uploaded over the internal network.

  • multipartUploadLimit: specifies the file size limit for multipart upload. Unit: bytes. Default value: 10 MB. This parameter is valid only for video uploads.

  • multipartUploadOnceSize: specifies the size of each part in multipart upload. Unit: bytes. Default value: 10 MB. This parameter is valid only for video uploads.

  • tmpDir: specifies a local directory to temporarily store the files. This parameter is valid only for the upload of online files.

uploadLocalVideo

The API operation used to upload local videos.

uploadWebVideo

The API operation used to upload online videos.

uploadLocalImage

The API operation used to upload local images.

uploadWebImage

The API operation used to upload online images.

uploadLocalAttachedMedia

The API operation used to upload local auxiliary media assets.

uploadWebAttachedMedia

The API operation used to upload online auxiliary media assets.

uploadLocalM3u8

The API operation used to upload local M3U8 videos.

uploadWebM3u8

The API operation used to upload online M3U8 videos.

/VodSDK-C_1.0.0.gz/VodSDK-C_1.0.0/aliyun-c-sdk-vod/samples

Directory

Description

uploadVideo.cpp

The sample code for uploading videos.

uploadImage.cpp

The sample code for uploading images.

uploadAttachedMedia.cpp

The sample code for uploading auxiliary media assets.

Scenario 1: Upload audio and video files

Regular audio and video files

ApsaraVideo VOD allows you to upload audio or video files by using one of the following methods:

  • Use multipart upload to upload a local file. You can upload a file of up to 48.8 TB in size. Resumable upload is not supported. For more information, see the testUploadLocalVideo function in the sample code.

  • Use the file URL to upload an online file. You can upload a file of up to 48.8 TB in size. Before you upload online files, you must download the files to a local disk. Make sure that the local disk has sufficient space. For more information, see the testUploadWebVideo function in the following sample code.

Show sample code

void testCallback(int64_t consumed_bytes, int64_t total_bytes)
{
    printf("total :%ld, %ld\n", consumed_bytes, total_bytes);
}
// Test the upload of a local video.
VodApiResponse testUploadLocalVideo(VodCredential authInfo) {
    CreateUploadVideoRequest request;
    // The name of the source file.
    request.fileName = "test.mp4";
    // The title of the video.
    request.title = "testVideo****";
    // The category ID of the video.
    request.cateId = "1";
    // The URL of the custom video thumbnail. Example: http://example.com/example-****.jpg.
    request.coverURL = "<your cover URL>";
    // The tags of the video.
    request.tags = "test1,test2";
    // The ID of the transcoding template group.
    request.templateGroupId = "6ae347b0140181ad371d197ebe28****";
    // The storage address. Example: example-bucket-****.oss-cn-shanghai.aliyuncs.com.
    requests.storageLocation = "<your torageLocation>";
    Json::Value userData;
    Json::Value callbackUrl;
    // The callback URL. Example: https://example.aliyundoc.com/ProcessMessageCallback.
    callbackUrl["CallbackURL"] = "<your callback URL>";
    userData["MessageCallback"] = callbackUrl;
    Json::Value extend;
    extend["localId"] = "xxx";
    extend["test"] = "www";
    userData["Extend"] = extend;
    request.userData = userData.toStyledString();
    UploadOptions uploadOptions;
    // Specify the region of the ECS instance where the upload script is deployed. If the ECS region is the same as the storage region of ApsaraVideo VOD, the file is automatically uploaded over the internal network.
    //uploadOptions.ecsRegionId = "cn-shanghai";
    // Configure a callback function for the upload progress. If you do not set this parameter, the default callback configurations are used. If you set this parameter to NULL, no callback for the upload progress is invoked.
    //uploadOptions.uploadProgressCallback = testCallback;
    //uploadOptions.multipartUploadLimit = 20*1024*1024;// Specify the file size limit for multipart upload.
    //uploadOptions.multipartUploadOnceSize = 10*1024*1024;// Specify the size of each part in multipart upload.
    VodApiResponse result = uploadLocalVideo(authInfo, request, "./test.mp4", uploadOptions);
    return result;
}
// Test the upload of an online video.
VodApiResponse testUploadWebVideo(VodCredential authInfo) {
    CreateUploadVideoRequest request;
    request.fileName = "testWeb****.mp4";
    request.title = "testUploadWebVideo****";
    UploadOptions uploadOptions;
    // Specify the region of the ECS instance where the upload script is deployed. If the ECS region is the same as the storage region of ApsaraVideo VOD, the file is automatically uploaded over the internal network.
    //uploadOptions.ecsRegionId = "cn-shanghai";
    // Configure a callback function for the upload progress. If you do not set this parameter, the default callback configurations are used. If you set this parameter to NULL, no callback for the upload progress is invoked.
    //uploadOptions.uploadProgressCallback = testCallback;
    //uploadOptions.multipartUploadLimit = 20*1024*1024;// Specify the file size limit for multipart upload.
    //uploadOptions.multipartUploadOnceSize = 10*1024*1024;// Specify the size of each part in multipart upload.
    // Specify a local directory to temporarily store the downloaded online files. Default value: /tmp/.
    //uploadOptions.tmpDir = "/tmp/";
    VodApiResponse result = uploadWebVideo(authInfo, request, "<Your Download Url>", uploadOptions);
    return result;
}

####  Run the test code.   ####
VodCredential initVodClient(std::string accessKeyId, std::string accessKeySecret) {
    VodCredential authInfo;
    authInfo.accessKeyId = accessKeyId;
    authInfo.accessKeySecret = accessKeySecret;
    authInfo.regionId = "cn-shanghai";
    return authInfo;
}
int main(int argc, char * argv[]) {
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. 
    // We recommend that you not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
    // In this example, ApsaraVideo VOD reads the AccessKey pair from the environment variables to implement identity verification for API access. Before you run the sample code, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. 
    VodCredential authInfo = initVodClient(std:getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), std:getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
    VodApiResponse response;
    response = testUploadLocalVideo(authInfo);
    //response = testUploadWebVideo(authInfo);
    //response = testUploadLocalM3u8(authInfo);
    //response = testUploadWebM3u8(authInfo);
    printf("httpCode: %d, result: %s\n", response.httpCode, response.result.c_str());
}

M3U8 files

Show sample code

void testCallback(int64_t consumed_bytes, int64_t total_bytes)
{
    printf("total :%ld, %ld\n", consumed_bytes, total_bytes);
}
// Test the upload of a local M3U8 video.
VodApiResponse testUploadLocalM3u8(VodCredential authInfo) {
    CreateUploadVideoRequest request;
    // The name of the source file.
    request.fileName = "testLocal****.m3u8";
    // The title of the video.
    request.title = "testUploadLocalM3u8****";
    list<string> tsList;
    // If you do not specify the URLs of part files, the files are automatically parsed.
    //tsList.push_back("/tmp/1.ts");
    UploadOptions uploadOptions;
    VodApiResponse result = uploadLocalM3u8(authInfo, request, "./test****.m3u8", tsList, uploadOptions);
    return result;
}
// Test the upload of an online M3U8 video.
VodApiResponse testUploadWebM3u8(VodCredential authInfo) {
    CreateUploadVideoRequest request;
    // The name of the source file.
    request.fileName = "testWeb****.m3u8";
    // The title of the video.
    request.title = "testUploadWebM3u8****";
    list<string> tsList;
    // If you do not specify the URLs of part files, the files are automatically parsed.
    //tsList.push_back("<Ts1 Download Url>");
    //tsList.push_back("<Ts2 Download Url>");
    UploadOptions uploadOptions;
    VodApiResponse result = uploadWebM3u8(authInfo, request, "<Your M3u8 Download Url>", tsList, uploadOptions);
    return result;
}
####  Run the test code.   ####
VodCredential initVodClient(std::string accessKeyId, std::string accessKeySecret) {
    VodCredential authInfo;
    authInfo.accessKeyId = accessKeyId;
    authInfo.accessKeySecret = accessKeySecret;
    authInfo.regionId = "cn-shanghai";
    return authInfo;
}
int main(int argc, char * argv[]) {
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. 
    // We recommend that you not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
    // In this example, ApsaraVideo VOD reads the AccessKey pair from the environment variables to implement identity verification for API access. Before you run the sample code, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. 
    VodCredential authInfo = initVodClient(std:getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), std:getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
    VodApiResponse response;
    response = testUploadLocalVideo(authInfo);
    //response = testUploadWebVideo(authInfo);
    //response = testUploadLocalM3u8(authInfo);
    //response = testUploadWebM3u8(authInfo);
    printf("httpCode: %d, result: %s\n", response.httpCode, response.result.c_str());
}

Scenario 2: Upload images

Show sample code

void testCallback(int64_t consumed_bytes, int64_t total_bytes)
{
    printf("total :%ld, %ld\n", consumed_bytes, total_bytes);
}

VodApiResponse testUploadLocalImage(VodCredential authInfo)
{
    CreateUploadImageRequest request;
    // The type of the image.
    request.imageType = "default";
    // The title of the image.
    request.title = "testUploadLocalImage";
    UploadOptions uploadOptions;
    // Specify the region of the ECS instance where the upload script is deployed. If the ECS region is the same as the storage region of ApsaraVideo VOD, the file is automatically uploaded over the internal network.
    //uploadOptions.ecsRegionId = "cn-shanghai";
    // Configure a callback function for the upload progress. If you do not set this parameter, the default callback configurations are used. If you set this parameter to NULL, no callback for the upload progress is invoked.
    //uploadOptions.uploadProgressCallback = testCallback;
    VodApiResponse result = uploadLocalImage(authInfo, request, "./test.png", uploadOptions);
    return result;
}

VodApiResponse testUploadWebImage(VodCredential authInfo) {
    CreateUploadImageRequest request;
    request.imageType = "default";
    request.title = "testUploadWebImage";
    UploadOptions uploadOptions;
    // Specify the region of the ECS instance where the upload script is deployed. If the ECS region is the same as the storage region of ApsaraVideo VOD, the file is automatically uploaded over the internal network.
    //uploadOptions.ecsRegionId = "cn-shanghai";
    // Configure a callback function for the upload progress. If you do not set this parameter, the default callback configurations are used. If you set this parameter to NULL, no callback for the upload progress is invoked.
    //uploadOptions.uploadProgressCallback = testCallback;
    // Specify a local directory to temporarily store the downloaded online files. Default value: /tmp/.
    //uploadOptions.tmpDir = "/tmp/";
    VodApiResponse result = uploadWebImage(authInfo, request, "<Your Download Url>", uploadOptions);
    return result;
}

VodCredential initVodClient(std::string accessKeyId, std::string accessKeySecret) {
    VodCredential authInfo;
    authInfo.accessKeyId = accessKeyId;
    authInfo.accessKeySecret = accessKeySecret;
    authInfo.regionId = "cn-shanghai";
    return authInfo;
}

Show sample code

int main(int argc, char * argv[]) {
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. 
    // We recommend that you not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
    // In this example, ApsaraVideo VOD reads the AccessKey pair from the environment variables to implement identity verification for API access. Before you run the sample code, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. 
    VodCredential authInfo = initVodClient(std:getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), std:getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
    VodApiResponse response;

    response = testUploadLocalImage(authInfo);
    //response = testUploadWebImage(authInfo);
    printf("httpCode: %d, result: %s\n", response.httpCode, response.result.c_str());
}

Scenario 3: Upload auxiliary media assets

Show sample code

void testCallback(int64_t consumed_bytes, int64_t total_bytes)
{
    printf("total :%ld, %ld\n", consumed_bytes, total_bytes);
}

VodApiResponse testUploadLocalAttachedMedia(VodCredential authInfo)
{
    CreateUploadAttachedMediaRequest request;
    // The type of the auxiliary media asset.
    request.businessType = "watermark";'
    // The file name extension.
    request.mediaExt = "png";
    // The title of the auxiliary media asset.
    request.title = "testUploadLocalAttachedMedia";
    UploadOptions uploadOptions;
    // Specify the region of the ECS instance where the upload script is deployed. If the ECS region is the same as the storage region of ApsaraVideo VOD, the file is automatically uploaded over the internal network.
    //uploadOptions.ecsRegionId = "cn-shanghai";
    // Configure a callback function for the upload progress. If you do not set this parameter, the default callback configurations are used. If you set this parameter to NULL, no callback for the upload progress is invoked.
    //uploadOptions.uploadProgressCallback = testCallback;
    return uploadLocalAttachedMedia(authInfo, request, "./test.png", uploadOptions);
}

VodApiResponse testUploadWebAttachedMedia(VodCredential authInfo)
{
    CreateUploadAttachedMediaRequest request;
    // The type of the auxiliary media asset.
    request.businessType = "watermark";
    // The file name extension.
    request.mediaExt = "png";
    // The title of the auxiliary media asset.
    request.title = "testUploadWebAttachedMedia";
    UploadOptions uploadOptions;
    // Specify the region of the ECS instance where the upload script is deployed. If the ECS region is the same as the storage region of ApsaraVideo VOD, the file is automatically uploaded over the internal network.
    //uploadOptions.ecsRegionId = "cn-shanghai";
    // Configure a callback function for the upload progress. If you do not set this parameter, the default callback configurations are used. If you set this parameter to NULL, no callback for the upload progress is invoked.
    //uploadOptions.uploadProgressCallback = testCallback;
    // Specify a local directory to temporarily store the downloaded online files. Default value: /tmp/.
    //uploadOptions.tmpDir = "/tmp/";
    return uploadWebAttachedMedia(authInfo, request, "<Your Download Url>", uploadOptions);
}

VodCredential initVodClient(std::string accessKeyId, std::string accessKeySecret) {
    VodCredential authInfo;
    authInfo.accessKeyId = accessKeyId;
    authInfo.accessKeySecret = accessKeySecret;
    authInfo.regionId = "cn-shanghai";
    return authInfo;
}

Show sample code

int main(int argc, char * argv[]) {
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. 
    // We recommend that you not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
    // In this example, ApsaraVideo VOD reads the AccessKey pair from the environment variables to implement identity verification for API access. Before you run the sample code, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. 
    VodCredential authInfo = initVodClient(std:getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), std:getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
    VodApiResponse response;
    response = testUploadLocalAttachedMedia(authInfo);
    //response = testUploadWebAttachedMedia(authInfo);
    printf("httpCode: %d, result: %s\n", response.httpCode, response.result.c_str());
}

References

Overview

STS SDK overview