SDK for Java

Last updated: 2020-08-17 15:34:57

    VOD provides an SDK for Java for uploading videos from a server. For more information on the upload process, please see Guide.

    Integration Methods

    Importing Maven dependency

    Add the VOD SDK dependency in the pom.xml file of your project.

    <dependency>
        <groupId>com.qcloud</groupId>
        <artifactId>vod_api</artifactId>
        <version>2.1.4</version>
    </dependency>

    Importing jar packages

    If Maven is not used for dependency management in your project, you can directly download the required jar packages and import them into the project:

    jar File Description
    vod_api-2.1.4.jar VOD SDK.
    jackson-annotations-2.9.0.jar,jackson-core-2.9.7.jar,jackson-databind-2.9.7.jar,gson-2.2.4.jar Open-source JSON libraries.
    cos_api-5.4.10.jar COS SDK.
    tencentcloud-sdk-java-3.1.2.jar TencentCloud API SDK.
    commons-codec-1.10.jar,commons-logging-1.2.jar,log4j-1.2.17.jar,slf4j-api-1.7.21.jar,slf4j-log4j12-1.7.21.jar Open-source log libraries.
    httpclient-4.5.3.jar,httpcore-4.4.6.jar,okhttp-2.5.0.jar,okio-1.6.0.jar Open-source HTTP processing libraries.
    joda-time-2.9.9.jar Open-source time processing library.
    jaxb-api-2.3.0.jar Open-source XML processing library.
    bcprov-jdk15on-1.59.jar Open-source encryption processing library.

    Download the jar packages associated with the SDK for Java here and import them into your project.

    Simple Upload

    Initializing upload client object

    Initialize a VodUploadClient instance with a TencentCloud API key.

    VodUploadClient client = new VodUploadClient("your secretId", "your secretKey");

    Constructing upload request object

    Set the local media upload path.

    VodUploadRequest request = new VodUploadRequest();
    request.setMediaFilePath("/data/videos/Wildlife.wmv");

    Calling upload method

    Call the upload method and pass in the access point region and upload request.

    try {
        VodUploadResponse response = client.upload("ap-guangzhou", request);
        logger.info("Upload FileId = {}", response.getFileId());
    } catch (Exception e) {
        // The business team performs troubleshooting
        logger.error("Upload Err", e);
    }

    Note:

    The upload method automatically selects simple upload or multipart upload based on the file size, eliminating your need to take care of every step in multipart upload.

    Advanced Features

    Uploading cover

    VodUploadClient client = new VodUploadClient("your secretId", "your secretKey");
    VodUploadRequest request = new VodUploadRequest();
    request.setMediaFilePath("/data/videos/Wildlife.wmv");
    request.setCoverFilePath("/data/videos/Wildlife.jpg");
    try {
        VodUploadResponse response = client.upload("ap-guangzhou", request);
        logger.info("Upload FileId = {}", response.getFileId());
    } catch (Exception e) {
        // The business team performs troubleshooting
        logger.error("Upload Err", e);
    }

    Specifying task flow

    First, create a task flow template and name it. When initiating the task flow, you can set the Procedure parameter with the task flow template name, and the task flow will be executed automatically upon upload success.

    VodUploadClient client = new VodUploadClient("your secretId", "your secretKey");
    VodUploadRequest request = new VodUploadRequest();
    request.setMediaFilePath("/data/videos/Wildlife.wmv");
    request.setProcedure("Your Procedure Name");
    try {
        VodUploadResponse response = client.upload("ap-guangzhou", request);
        logger.info("Upload FileId = {}", response.getFileId());
    } catch (Exception e) {
        // The business team performs troubleshooting
        logger.error("Upload Err", e);
    }

    Uploading to subapplication

    Pass in a subapplication ID. After the upload is successful, the resource will belong only to the specified subapplication.

    VodUploadClient client = new VodUploadClient("your secretId", "your secretKey");
    VodUploadRequest request = new VodUploadRequest();
    request.setMediaFilePath("/data/videos/Wildlife.wmv");
    request.setSubAppId(101);
    try {
        VodUploadResponse response = client.upload("ap-guangzhou", request);
        logger.info("Upload FileId = {}", response.getFileId());
    } catch (Exception e) {
        // The business team performs troubleshooting
        logger.error("Upload Err", e);
    }

    Specifying storage region

    In the console, confirm that the target storage region has been activated. If not, you can do so as instructed in Upload Storage Settings and then set the abbreviation of the storage region through the StorageRegion attribute.

    VodUploadClient client = new VodUploadClient("your secretId", "your secretKey");
    VodUploadRequest request = new VodUploadRequest();
    request.setMediaFilePath("/data/videos/Wildlife.wmv");
    request.setStorageRegion("ap-chongqing");
    try {
        VodUploadResponse response = client.upload("ap-guangzhou", request);
        logger.info("Upload FileId = {}", response.getFileId());
    } catch (Exception e) {
        // The business team performs troubleshooting
        logger.error("Upload Err", e);
    }

    Specifying the number of concurrent parts

    The number of concurrent parts is applicable to uploading a large file in multiple parts simultaneously. The advantage of multipart upload lies in that a large file can be uploaded quickly. The SDK automatically selects simple upload or multipart upload based on the file size, eliminating your need to take care of every step in multipart upload. The number of concurrent parts of the file is specified by the ConcurrentUploadNumber parameter.

    VodUploadClient client = new VodUploadClient("your secretId", "your secretKey");
    VodUploadRequest request = new VodUploadRequest();
    request.setMediaFilePath("/data/videos/Wildlife.wmv");
    request.setConcurrentUploadNumber(5);
    try {
        VodUploadResponse response = client.upload("ap-guangzhou", request);
        logger.info("Upload FileId = {}", response.getFileId());
    } catch (Exception e) {
        // The business team performs troubleshooting
        logger.error("Upload Err", e);
    }

    Uploading with temporary credentials

    Pass in the relevant key information of the temporary credentials to use the temporary credentials for authentication and upload.

    VodUploadClient client = new VodUploadClient("Credentials TmpSecretId", "Credentials TmpSecretKey", "Credentials Token");
    VodUploadRequest request = new VodUploadRequest();
    request.setMediaFilePath("/data/videos/Wildlife.wmv");
    try {
        VodUploadResponse response = client.upload("ap-guangzhou", request);
        logger.info("Upload FileId = {}", response.getFileId());
    } catch (Exception e) {
        // The business team performs troubleshooting
        logger.error("Upload Err", e);
    }

    Setting upload proxy

    Set an upload proxy, and then the protocol and data involved will be processed by the proxy. In this way, you can use the proxy to upload files to Tencent Cloud over your organization's private network.

    VodUploadClient client = new VodUploadClient("your secretId", "your secretKey");
    VodUploadRequest request = new VodUploadRequest();
    request.setMediaFilePath("/data/videos/Wildlife.wmv");
    HttpProfile httpProfile = new HttpProfile();
    httpProfile.setProxyHost("your proxy ip");
    httpProfile.setProxyPort(8080); //your proxy port
    client.setHttpProfile(httpProfile);
    try {
        VodUploadResponse response = client.upload("ap-guangzhou", request);
        logger.info("Upload FileId = {}", response.getFileId());
    } catch (Exception e) {
        // The business team performs troubleshooting
        logger.error("Upload Err", e);
    }

    Uploading adaptive bitstream file

    The adaptive bitstream formats supported by this SDK for upload include HLS and DASH, and the media files referenced by the manifest (M3U8 or MPD) must be relative paths (i.e., URLs and absolute paths cannot be used) and be located in the same-level directory or subdirectory of manifest (i.e., ../ cannot be used). When calling the SDK's upload APIs, enter the manifest path as the MediaFilePath parameter, and the SDK will parse the list of related media files and upload them together.

    VodUploadClient client = new VodUploadClient("your secretId", "your secretKey");
    VodUploadRequest request = new VodUploadRequest();
    request.setMediaFilePath("/data/videos/prog_index.m3u8");
    try {
        VodUploadResponse response = client.upload("ap-guangzhou", request);
        logger.info("Upload FileId = {}", response.getFileId());
    } catch (Exception e) {
        // The business team performs troubleshooting
        logger.error("Upload Err", e);
    }

    API Description

    Upload client class VodUploadClient

    Attribute Name Attribute Description Type Required
    secretId TencentCloud API key ID. String Yes
    secretKey TencentCloud API key. String Yes

    Upload request class VodUploadRequest

    Attribute Name Attribute Description Type Required
    MediaFilePath Path of the media file to be uploaded, which must be a local path and does not support URLs. String Yes
    MediaType Type of the media file to be uploaded. For the valid values, please see Overview of media upload. If the MediaFilePath path contains a file extension, this parameter can be left empty. String No
    MediaName Name of the media file after being uploaded. If this parameter is left empty, the filename in MediaFilePath will be used by default. String No
    CoverFilePath Path of the cover file to be uploaded, which must be a local path and does not support URLs. String No
    CoverType Type of the cover file to be uploaded. For the valid values, please see Overview of media upload. If the CoverFilePath path contains a file extension, this parameter can be left empty. String No
    Procedure Name of the task flow to be automatically executed after upload is completed. This parameter is specified when the task flow is created through the API or console. For more information, please see Task Flow. String No
    ExpireTime Expiration time of media file in ISO 8601 format. For more information, please see the notes on ISO date format. String No
    ClassId Category ID, which is used to categorize the media for management. A category can be created, and its ID can be obtained by using the CreateClass API. Integer No
    SourceContext Source context of up to 250 characters, which is used to pass through the user request information and will be returned by the upload callback API. String No
    SubAppId ID of subapplication in VOD. If you need to access a resource in a subapplication, enter the subapplication ID in this field; otherwise, leave it empty. Integer No
    StorageRegion Storage region, which specifies the region where to store the file. This field should be filled in with a region abbreviation. String No
    ConcurrentUploadNumber Number of concurrent parts, which is valid when a large file is uploaded in multiple parts. Integer No

    Upload response class VodUploadResponse

    Attribute Name Attribute Description Type
    FileId Unique ID of media file. String
    MediaUrl Media playback address. String
    CoverUrl Media cover address. String
    RequestId Unique ID of request. Each request returns a unique ID. The RequestId is required to troubleshoot issues. String

    Upload method VodUploadClient.upload(String region, VodUploadRequest request)

    Parameter Name Description Type Required
    region Access point region, i.e., the region where to request a VOD server. This is different from the storage region. For more information, please see the list of supported regions. String Yes
    request Upload request. VodUploadRequest Yes

    Error Codes

    Status Code Description
    InternalError Internal error.
    InvalidParameter.ExpireTime Incorrect parameter value: expiration time.
    InvalidParameterValue.CoverType Incorrect parameter value: cover type.
    InvalidParameterValue.MediaType Incorrect parameter value: media type.
    InvalidParameterValue.SubAppId Incorrect parameter value: subapplication ID.
    InvalidParameterValue.VodSessionKey Incorrect parameter value: VOD session.
    ResourceNotFound The resource does not exist.

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help