Coder Social home page Coder Social logo

Comments (4)

olgarithms avatar olgarithms commented on August 15, 2024

Check KeeneState's open issue "sample code generates a 400 bad request #117".

from azure-sdk-for-media-services.

itorian avatar itorian commented on August 15, 2024

I already resolved this issue using similar way.

from azure-sdk-for-media-services.

olgarithms avatar olgarithms commented on August 15, 2024

Great, I am facing problems in the next step. Could you tell me how you solved this, maybe it would help me out.
Thanks in advance!

from azure-sdk-for-media-services.

itorian avatar itorian commented on August 15, 2024

Sure, here's my working code on production (var names & message changed):

private IAsset CreateMediaAsset(CloudFile model, string lecId)
{
    CloudMediaContext context = new CloudMediaContext(mediaAccountName, mediaAccountKey);

    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer mediaBlobContainer = cloudBlobClient.GetContainerReference(storageContainerReference);

    mediaBlobContainer.CreateIfNotExists();

    // Create a new asset.
    IAsset asset = context.Assets.Create("Lec-" + lecId + "-" + Guid.NewGuid(), AssetCreationOptions.None);
    IAccessPolicy writePolicy = context.AccessPolicies.Create("writePolicy", TimeSpan.FromMinutes(120), AccessPermissions.Write);
    ILocator destinationLocator = context.Locators.CreateLocator(LocatorType.Sas, asset, writePolicy);

    // Get the asset container URI and copy blobs from mediaContainer to assetContainer
    Uri uploadUri = new Uri(destinationLocator.Path);
    string assetContainerName = uploadUri.Segments[1];
    CloudBlobContainer assetContainer = cloudBlobClient.GetContainerReference(assetContainerName);
    string fileName = HttpUtility.UrlDecode(Path.GetFileName(model.BlockBlob.Uri.AbsoluteUri));

    var sourceCloudBlob = mediaBlobContainer.GetBlockBlobReference(fileName);
    sourceCloudBlob.FetchAttributes();

    if (sourceCloudBlob.Properties.Length > 0)
    {
        IAssetFile assetFile = asset.AssetFiles.Create(fileName);
        var destinationBlob = assetContainer.GetBlockBlobReference(fileName);

        destinationBlob.DeleteIfExists();
        destinationBlob.StartCopyFromBlob(sourceCloudBlob);
        destinationBlob.FetchAttributes();
        if (sourceCloudBlob.Properties.Length != destinationBlob.Properties.Length)
            model.UploadStatusMessage += "Failed to copy as Media Asset!";
    }

    // Delete destination locator as we want our assets to access through media services only
    destinationLocator.Delete();
    writePolicy.Delete();

    // Delete temp blob to free up chunked uploaded space
    sourceCloudBlob.Delete();

    // Refresh the new asset
    asset = context.Assets.Where(a => a.Id == asset.Id).FirstOrDefault();

    var ismAssetFiles = asset.AssetFiles.ToList().Where(f => f.Name.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase)).ToArray();

    if (ismAssetFiles.Count() != 1)
        throw new ArgumentException("The asset should have only one, .ism file");

    ismAssetFiles.First().IsPrimary = true;
    ismAssetFiles.First().Update();

    model.UploadStatusMessage += " File uploaded successfully by id: " + asset.Id;
    model.AssetId = asset.Id;

    return asset;
}

Hope this helps, let me know if you still face issue.

from azure-sdk-for-media-services.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.