Azure / azure-sdk-for-media-services

Azure SDK for Media Services
65 stars 65 forks source link

Migration from 2.3.0.1 to 3.5.1.0 caused issue in working code #131

Open itorian opened 9 years ago

itorian commented 9 years ago

Is there any migration documentation on windowsazure.mediaservices 2.3.0.1 to windowsazure.mediaservices 3.5.1.0? I just updated this package and noticed container.CreateIfNotExists() started displaying error "The remote server returned an error: (400) Bad Request.". Next, when I noticed this issue, i tried to add a new package windowsazure.mediaservices.extensions 3.3.0 instead of above migration. This also produced same error. I need updated package for encoding & job scheduling stuff. Let me know if this is possible even without migration or extension package?

olgarithms commented 9 years ago

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

itorian commented 9 years ago

I already resolved this issue using similar way.

olgarithms commented 9 years ago

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!

itorian commented 9 years ago

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.