All Products
Search
Document Center

ApsaraVideo VOD:Media upload

Last Updated:May 19, 2022

ApsaraVideo VOD servers provide a variety of API operations for media upload. You can upload media files to ApsaraVideo VOD by calling one of these API operations. This topic describes the scenarios in which you can use the server operation SDK for .NETY to call these operations and provides sample code.

Scenarios

Notice

This topic provides only the sample code of calling API operations, such as the operation for obtaining a URL and a credential for uploading a media file. For more information about the scenarios in which the operations can be used, see the following table.

Operation

Scenario

CreateUploadVideo

  • If you upload media files by using Object Storage Service (OSS) SDKs, you need to use the server operation SDK and call an operation to obtain a URL and a credential for uploading a media file. The obtained URL and credential are Base64-encoded and must be decoded before they are used to initialize an OSS client. For more information, see Upload media files by using OSS SDKs.

  • If you upload media file by using ApsaraVideo VOD clients, you need to use the server operation SDK and call an operation to obtain a URL and a credential for uploading a media file. You can deliver the URL and credential to clients without the need to decode the URL or credential. For more information, see Upload from clients.

RefreshUploadVideo

CreateUploadImage

CreateUploadAttachedMedia

UploadMediaByURL

  • You can call the UploadMediaByURL operation to upload media files by using the URLs of source files. This way, you do not need to download media files to your servers or terminals before you use the server operation SDK to upload the media files to ApsaraVideo VOD.

  • You can call the GetURLUploadInfos operation to query the information about URL-based upload jobs.

GetURLUploadInfos

RegisterMedia

  • After you upload media files to a user-created OSS bucket, you can call the RegisterMedia operation to register the media files and obtain the media IDs of the files. After you register the media files, you can submit transcoding or snapshot jobs for the media files. For more information, see Add a user-created OSS bucket.

Prerequisites

  • The server operation SDK for .NET is installed. For more information, see Installation.

  • The server operation SDK for .NET is initialized. For more information, see Initialization.

    Note

    In the following examples, an AccessKey pair is used to initialize the server operation SDK.

Sample code

Obtain a URL and a credential for uploading an audio file or a video

You can call the CreateUploadVideo operation to obtain a URL and a credential for uploading an audio file or a video.

For more information about the request and response parameters of this operation, see CreateUploadVideo. The following sample code provides an example:

using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.vod.Model.V20170321;

namespace Aliyun.Acs.vod.Sdk.CreateUploadVideo
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            try
            {
                // Construct a request.
                CreateUploadVideoRequest request = new CreateUploadVideoRequest();
                request.Title = "this is a sample title";
                request.FileName = "sample.mp4";
                // request.Tags = "tags1,tags2";
                // request.Description = "this is a sample description";
                // The thumbnail URL. Example: http://192.168.0.0/16/sample****.jpg.
                // request.CoverURL = "<your Cover URL>";
                // request.CateId = -1;
                // request.TemplateGroupId = "278840921dee4963bb5862b43a52****";
                // Initialize a client.
                DefaultAcsClient client = InitVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
                // Initiate the request and display the response.
                CreateUploadVideoResponse response = client.GetAcsResponse(request);
                Console.WriteLine("RequestId = " + response.RequestId);
                Console.WriteLine("UploadAddress = " + response.UploadAddress);
                Console.WriteLine("UploadAuth = " + response.UploadAuth);
            }
            catch (ServerException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            catch (ClientException ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}
Notice

When you call the CreateUploadVideo operation to create a credential, a URL, and a video ID for uploading a video, the video is in the Uploading state in the ApsaraVideo VOD console. The video remains in the Uploading state in the ApsaraVideo VOD console in the following cases: You do not upload the video in the client, an error occurs during the upload, or the upload job is interrupted, for example, the application process is terminated. In these cases, call the RefreshUploadVideo operation to pass the video ID to obtain a new URL and credential, and then upload the video. Alternatively, manually delete the video that is in the Uploading state. For more information, see RefreshUploadVideo. To upload videos, call the UploadMediaByURL operation.

Refresh the credential for uploading an audio file or a video

You can call the RefreshUploadVideo operation to refresh the credential for uploading an audio file or a video.

For more information about the request and response parameters of this operation, see RefreshUploadVideo. The following sample code provides an example:

using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.vod.Model.V20170321;

namespace Aliyun.Acs.vod.Sdk.Sample
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            try
            {
                // Construct a request.
                RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
                request.VideoId = "Video ID";
                // Initialize a client.
                DefaultAcsClient client = InitVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
                // Initiate the request and display the response.
                RefreshUploadVideoResponse response = client.GetAcsResponse(request);
                Console.WriteLine("RequestId = " + response.RequestId);
                Console.WriteLine("UploadAddress = " + response.UploadAddress);
                Console.WriteLine("UploadAuth = " + response.UploadAuth);
            }
            catch (ServerException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            catch (ClientException ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

Obtain a URL and a credential for uploading an image

You can call the CreateUploadImage operation to obtain a URL and a credential for uploading an image.

For more information about the request and response parameters of this operation, see CreateUploadImage. The following sample code provides an example:

using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.vod.Model.V20170321;

namespace Aliyun.Acs.vod.Sdk.Sample
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            try
            {
                // Construct a request.
                CreateUploadImageRequest request = new CreateUploadImageRequest();
                request.ImageType = "cover";
                request.ImageExt = "jpg";
                // Initialize a client.
                DefaultAcsClient client = InitVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
                // Initiate the request and display the response.
                CreateUploadImageResponse response = client.GetAcsResponse(request);
                Console.WriteLine("RequestId = " + response.RequestId);
                Console.WriteLine("UploadAddress = " + response.UploadAddress);
                Console.WriteLine("UploadAuth = " + response.UploadAuth);
            }
            catch (ServerException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            catch (ClientException ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

Obtain a URL and a credential for uploading an auxiliary media asset

You can call the CreateUploadAttachedMedia operation to obtain a URL and a credential for uploading an auxiliary media asset.

For more information about the request and response parameters of this operation, see CreateUploadAttachedMedia. The following sample code provides an example:

using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.vod.Model.V20170321;

namespace CreateUploadAttachedMedia
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            try
            {
                // Construct a request.
                CreateUploadAttachedMediaRequest request = new CreateUploadAttachedMediaRequest();
                request.BusinessType = "watermark";
                request.MediaExt = "gif";
                request.Title = "this is a sample";
                // Initialize a client.
                DefaultAcsClient client = InitVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
                // Initiate the request and display the response.
                CreateUploadAttachedMediaResponse response = client.GetAcsResponse(request);
                Console.WriteLine("RequestId = " + response.RequestId);
                Console.WriteLine("MediaId = " + response.MediaId);
                Console.WriteLine("MediaURL = " + response.MediaURL);
                Console.WriteLine("UploadAddress = " + response.UploadAddress);
                Console.WriteLine("UploadAuth = " + response.UploadAuth);
            }
            catch (ServerException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            catch (ClientException ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

Upload multiple media files by using the URLs of source files at a time

You can call the UploadMediaByURL operation to upload multiple media files by using the URLs of source files at a time.

For more information about the request and response parameters of this operation, see UploadMediaByURL. The following sample code provides an example:

using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.vod.Model.V20170321;

namespace Aliyun.Acs.vod.Sdk.UploadMediaByURL
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            try
            {
                UploadMediaByURLRequest request = new UploadMediaByURLRequest();
                // The URL of the source file. Example: http://192.168.0.0/16/test****.mp4.
                request.UploadMetadatas = "[{\"SourceUrl\":\"<your Source URL>\",\"Title\":\"upload by url sample\"}]";
                // Initialize a client.
                DefaultAcsClient client = InitVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
                // Initiate the request and display the response.
                UploadMediaByURLResponse response = client.GetAcsResponse(request);
                Console.WriteLine("RequestId = " + response.RequestId);
            }
            catch (ServerException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            catch (ClientException ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

Register media assets

You can call the RegisterMedia operation to register media assets.

For more information about the request and response parameters of this operation, see RegisterMedia. The following sample code provides an example:

using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.vod.Model.V20170321;

namespace Aliyun.Acs.vod.Sdk.RegisterMedia
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            try
            {
                RegisterMediaRequest request = new RegisterMediaRequest();
                // The OSS URL of the media file. Example: https://example-bucket-****.oss-cn-shanghai.aliyuncs.com/vod-test/aaa****.mp4.
                request.RegisterMetadatas = "[{\"Title\":\"register media test\",\"FileURL\":\"<your File URL>\"}]";
                // Initialize a client.
                DefaultAcsClient client = InitVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
                // Initiate the request and display the response.
                RegisterMediaResponse response = client.GetAcsResponse(request);
                Console.WriteLine("RequestId = " + response.RequestId);
                if (response.RegisteredMediaList != null && response.RegisteredMediaList.Count > 0) {
                    foreach(var media in response.RegisteredMediaList) {
                        Console.WriteLine("MediaId = " + media.MediaId);
                        Console.WriteLine("FileURL = " + media.FileURL);
                        Console.WriteLine("NewRegister = " + media.NewRegister);
                    }
                }
                if (response.FailedFileURLs != null && response.FailedFileURLs.Count > 0) {
                    foreach (var fileURL in response.FailedFileURLs) {
                        Console.WriteLine("FailedFileURL = " + fileURL);
                    }
                }
            }
            catch (ServerException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            catch (ClientException ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}