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

Upload process

The internal upload logic used by the server upload SDK for C or C++ is the same as that of the general upload process of server upload SDKs. For more information, see the "Upload process" section of the Upload process topic. The following content describes the basic process of using the server upload SDK for C or C++:
  1. Complete the prerequisites. For more information, see the Prerequisites section.
  2. Integrate the upload SDK for C or C++. For more information, see the Integrate the upload SDK for C or C++ section.
  3. Implement the upload logic. This involves upload information configuration.
    • For more information about the sample code for uploading audio and video files, see the Upload audio and video files section.
    • For more information about the sample code for uploading images, see the Upload images section.
    • For more information about the sample code for uploading auxiliary media assets, see the Upload auxiliary media assets section.

Prerequisites

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

  • The system settings required for uploading media files, including the OSS bucket for storing the media files in the specified region and the callback settings, are configured. For more information, see Manage VOD resources and Configure callback settings.
  • 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 Create a role and grant temporary access permissions to the role by using STS.
    Note For more information about the scenarios in which STS can be used, see Comparison between credentials and STS.

Integrate the upload SDK for C or C++

Note
  • 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 upload SDK for C or C++ and sample code after decompression, see the Directories of the upload SDK for C or C++ section.
  1. Download the VodSDK-C_1.0.0.gz package, which contains the upload SDK for C or C++ and sample code. For more information, see SDK download.
  2. Install the upload SDK for C or C++ based on the installation method of the server SDK for C or C++. For more information, see Installation.

Update the upload SDK for C or C++

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

Note You can check the version number and release date of the current SDK in the first line of the ChangeLog.txt file in the aliyun-c-sdk-vod directory.

Upload audio and video files

Ordinary audio and video files
The upload SDK for C or C++ supports the following types of audio and video files:
  • Local files. Multipart upload is used. A single file to be uploaded can be up to 48.8 TB in size. Resumable upload is not supported. For more information, see the testUploadLocalVideo function in the following sample code.
  • Online files. You can specify upload URLs to upload online files. A single file to be uploaded can be 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.
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 video 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 location. 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 Elastic Compute Service (ECS) instance on which the upload script is to be deployed. If the region of the ECS instance is the same as the storage region in ApsaraVideo VOD, the file is automatically uploaded over the internal network.
    //uploadOptions.ecsRegionId = "cn-shanghai";
    // Customize the callback for upload progress. If you do not set this parameter, the default callback is used. If you set this parameter to NULL, no callback for upload progress is invoked.
    //uploadOptions.uploadProgressCallback = testCallback;
    //uploadOptions.multipartUploadLimit = 20*1024*1024;// Specify the threshold of the file size based on which multipart upload starts.
    //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 on which the upload script is to be deployed. If the region of the ECS instance is the same as the storage region in ApsaraVideo VOD, the file is automatically uploaded over the internal network.
    //uploadOptions.ecsRegionId = "cn-shanghai";
    // Customize the callback for upload progress. If you do not set this parameter, the default callback is used. If you set this parameter to NULL, no callback for upload progress is invoked.
    //uploadOptions.uploadProgressCallback = testCallback;
    //uploadOptions.multipartUploadLimit = 20*1024*1024;// Specify the threshold of the file size based on which multipart upload starts.
    //uploadOptions.multipartUploadOnceSize = 10*1024*1024;// Specifies the size of each part in multipart upload.
    // Specify a local temporary directory for storing the downloaded online files. The default value is /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[]) {
    VodCredential authInfo = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    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
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 video 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 file parts, the file is 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 video 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 file parts, the file is 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[]) {
    VodCredential authInfo = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    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());
}

Upload images

Part of the sample code for uploading images:

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 on which the upload script is to be deployed. If the region of the ECS instance is the same as the storage region in ApsaraVideo VOD, the file is automatically uploaded over the internal network.
    //uploadOptions.ecsRegionId = "cn-shanghai";
    // Customize the callback for upload progress. If you do not set this parameter, the default callback is used. If you set this parameter to NULL, no callback for 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 on which the upload script is to be deployed. If the region of the ECS instance is the same as the storage region in ApsaraVideo VOD, the file is automatically uploaded over the internal network.
    //uploadOptions.ecsRegionId = "cn-shanghai";
    // Customize the callback for upload progress. If you do not set this parameter, the default callback is used. If you set this parameter to NULL, no callback for upload progress is invoked.
    //uploadOptions.uploadProgressCallback = testCallback;
    // Specify a local temporary directory for storing the downloaded online files. The default value is /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;
}
int main(int argc, char * argv[]) {
    VodCredential authInfo = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    VodApiResponse response;

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

Upload auxiliary media assets

Part of the sample code for uploading auxiliary media assets:

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 on which the upload script is to be deployed. If the region of the ECS instance is the same as the storage region in ApsaraVideo VOD, the file is automatically uploaded over the internal network.
    //uploadOptions.ecsRegionId = "cn-shanghai";
    // Customize the callback for upload progress. If you do not set this parameter, the default callback is used. If you set this parameter to NULL, no callback for 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 on which the upload script is to be deployed. If the region of the ECS instance is the same as the storage region in ApsaraVideo VOD, the file is automatically uploaded over the internal network.
    //uploadOptions.ecsRegionId = "cn-shanghai";
    // Customize the callback for upload progress. If you do not set this parameter, the default callback is used. If you set this parameter to NULL, no callback for upload progress is invoked.
    //uploadOptions.uploadProgressCallback = testCallback;
    // Specify a local temporary directory for storing the downloaded online files. The default value is /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;
}
int main(int argc, char * argv[]) {
    VodCredential authInfo = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    VodApiResponse response;
    response = testUploadLocalAttachedMedia(authInfo);
    //response = testUploadWebAttachedMedia(authInfo);
    printf("httpCode: %d, result: %s\n", response.httpCode, response.result.c_str());
}

Directories of the upload SDK for C or C++

/VodSDK-C_1.0.0.gz decompress directory/VodSDK-C_1.0.0/aliyun-c-sdk-vod/src/upload.h
Directory Description
CreateUploadVideoRequest The request class for uploading videos. For more information about the parameters, see CreateUploadVideo.
CreateUploadImageRequest The request class for uploading images. For more information about the parameters, see CreateUploadImage.
CreateUploadAttachedMediaRequest The request class for uploading auxiliary media assets. For more information about the parameters, see CreateUploadAttachedMedia.
UploadOptions The upload parameter structure, which contains the following parameters:
  • void (*uploadProgressCallback) (int64_t, int64_t): customizes the callbacks for upload progress. If you do not set this parameter, the default callback is used. If you set this parameter to NULL, no callback for upload progress is invoked.
  • ecsRegionId: specifies the region of the ECS instance on which the upload script is to be deployed. If the region of the ECS instance is the same as the storage region in ApsaraVideo VOD, the file is automatically uploaded over the internal network.
  • multipartUploadLimit: specifies the threshold of the file size based on which multipart upload starts, in bytes. The default value is 10 MB. This parameter is valid only for the upload of videos.
  • multipartUploadOnceSize: specifies the size of each part in multipart upload, in bytes. The default value is 10 MB. This parameter is valid only for the upload of videos.
  • tmpDir: specifies a local temporary directory for storing the downloaded online files. This parameter is valid only for the upload of online files.
uploadLocalVideo The method used to upload local videos.
uploadWebVideo The method used to upload online videos.
uploadLocalImage The method used to upload local images.
uploadWebImage The method used to upload online images.
uploadLocalAttachedMedia The method used to upload local auxiliary media assets.
uploadWebAttachedMedia The method used to upload online auxiliary media assets.
uploadLocalM3u8 The method used to upload local M3U8 videos.
uploadWebM3u8 The method used to upload online M3U8 videos.
/VodSDK-C_1.0.0.gz decompress directory/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.