Home > database >  Unable to upload to azure media service
Unable to upload to azure media service

Time:01-17

I have been trying to upload a file to Azure media server but i am getting the excpetion as

Azure Media Services endpoint Uri schema is not valid

I have tried below code

static void Main(string[] args)
{

    var configuration = new ConfigurationBuilder()
             .AddJsonFile("appsettings.json")
             .Build();
    //_cachedCredentials = new MediaServicesCredentials(_mediaServicesAccountName,
    //_mediaServicesAccountKey);
    //_context = new CloudMediaContext(_cachedCredentials);

    Program program = new Program(configuration);

    //var tokenCredentials1 = new AzureAdTokenCredentials(program._mediaServicesTenant, AzureEnvironments.AzureCloudEnvironment);
    //var tokenProvider1 = new AzureAdTokenProvider(tokenCredentials1);
    //var mediaContext = new CloudMediaContext(new Uri(program._mediaServicesURL), tokenProvider1);
    //mediaContext.Assets.FirstOrDefault();


    AzureAdTokenCredentials tokenCredentials =
               new AzureAdTokenCredentials(program._mediaServicesTenant,
                   new AzureAdClientSymmetricKey(program._mediaServicesClientID, program._mediaServicesClientSecret),
                   AzureEnvironments.AzureCloudEnvironment);

    var tokenProvider = new AzureAdTokenProvider(tokenCredentials);

    //program._mediaServicesURL is "https://management.azure.com/"
    _context = new CloudMediaContext(new Uri(program._mediaServicesURL), tokenProvider);



    IAsset inputAsset = CreateAssetAndUploadSingleFile(@"E:\User\SampleVideo.mp4", AssetCreationOptions.CommonEncryptionProtected);

    //EncodeToAdaptiveBitrateMP4Set(inputAsset);
}
 

 static public IAsset CreateAssetAndUploadSingleFile(string singleFilePath, AssetCreationOptions assetCreationOptions)
{
    if (!File.Exists(singleFilePath))
    {
        Console.WriteLine("File does not exist.");
        return null;
    }

    var assetName = Path.GetFileNameWithoutExtension(singleFilePath);
    //create a new input asset
    IAsset inputAsset = _context.Assets.Create(assetName, assetCreationOptions);

    var assetFile = inputAsset.AssetFiles.Create(Path.GetFileName(singleFilePath));

    Console.WriteLine("Created assetFile {0}", assetFile.Name);

    //create a 30-day read and list access policy
    var policy = _context.AccessPolicies.Create(
    assetName,
    TimeSpan.FromDays(30),
    AccessPermissions.Read | AccessPermissions.List);

    //create a SAS locator to display the asset
    var locator = _context.Locators.CreateLocator(LocatorType.Sas, inputAsset, policy);

    Console.WriteLine("Upload {0}", assetFile.Name);

    assetFile.Upload(singleFilePath);

    Console.WriteLine("Done uploading {0}", assetFile.Name);

    locator.Delete();
    policy.Delete();

    return inputAsset;
}

Please Help me out i have seen lot of articles but no luck can anyone provide the complete upload working code if possible

CodePudding user response:

It looks like you are using the old legacy deprecated v2 API. Was that intentional or a mistake? If a mistake how did you arrive there?

Can you try to use the latest v3 API from the samples here: https://learn.microsoft.com/en-us/azure/media-services/latest/samples-overview

  • Related