All Products
Search
Document Center

Media upload

Last Updated: Jan 04, 2021

This topic provides examples on how to use the API operations of the media upload module. The API operations are encapsulated in ApsaraVideo VOD SDK for .NET. You can call the API operations to create upload URLs and credentials and register media assets. You can also upload media files by using a source file URL.

Initialize a client

Before you can use the SDK, initialize a client. For more information, see Initialization.

Create a URL and a credential for uploading videos

You can call the CreateUploadVideo operation to create a URL and a credential for uploading videos.

For more information about the request and response parameters of this operation, see CreateUploadVideo. 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
            {
                // Create 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";
                // request.CoverURL = "http://192.168.0.0/16/sample.jpg";
                // 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());
            }
        }
    }
}

Refresh the credential for uploading videos

You can call the RefreshUploadVideo operation to refresh the credential for uploading videos.

For more information about the request and response parameters of this operation, see RefreshUploadVideo. 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
            {
                // Create 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());
            }
        }
    }
}

Create a URL and a credential for uploading images

You can call the CreateUploadImage operation to create a URL and a credential for uploading images.

For more information about the request and response parameters of this operation, see CreateUploadImage. 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
            {
                // Create 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());
            }
        }
    }
}

Create a URL and a credential for uploading attached media assets

You can call the CreateUploadAttachedMedia operation to create a URL and a credential for uploading attached media assets.

For more information about the request and response parameters of this operation, see CreateUploadAttachedMedia. 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
            {
                // Create 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 media files by using a source file URL

You can call the UploadMediaByURL operation to upload media files by using a source file URL.

For more information about the request and response parameters of this operation, see UploadMediaByURL. 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();
                request.UploadMetadatas = "[{\"SourceUrl\":\"http://192.168.0.0/16/test.mp4\",\"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. 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();
                request.RegisterMetadatas = "[{\"Title\":\"register media test\",\"FileURL\":\"https://test.oss-cn-shanghai.aliyuncs.com/vod-test/aaa.mp4\"}]";
                // 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());
            }
        }
    }
}