Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sometimes get asset id null #200

Open
mobius127 opened this issue Mar 23, 2018 · 0 comments
Open

Sometimes get asset id null #200

mobius127 opened this issue Mar 23, 2018 · 0 comments

Comments

@mobius127
Copy link

At the previous stage of encoding, I have to copy the video form temp container an create a new Asset before encoding.

At this point, sometimes the new asset has null Id. This is my code:

`
public string CopyFileToMediaService(VideoModel video, string fileName)
{
try
{
if (video == null)
{
return null;
}

    var assetName = string.Format(CultureInfo.InvariantCulture, "NewAsset_{0}_{1}", video.VideoModelId, video.Name);

    IAsset newAsset = null;

    try
    {
        newAsset = this.mediaContext.Assets.Create(assetName, AssetCreationOptions.None);
    }
    catch (Exception ex)
    {
        this.logger.Error(string.Format(CultureInfo.InvariantCulture, ErrorMessages.EXCEPTION_ERROR, ex.StackTrace));

        Thread.Sleep(1000);
        newAsset = this.mediaContext.Assets.Create(assetName, AssetCreationOptions.None);
    }

            
    IAccessPolicy writePolicy = this.mediaContext.AccessPolicies.Create("writePolicy", TimeSpan.FromHours(24), AccessPermissions.Write);
    ILocator destinationLocator = this.mediaContext.Locators.CreateLocator(LocatorType.Sas, newAsset, writePolicy);

    string destinationContainerName = (new Uri(destinationLocator.Path)).Segments[1];
    CloudBlobClient destinationBlobClient = this.destinationStorageAccount.CreateCloudBlobClient();
    CloudBlobContainer assetContainer = destinationBlobClient.GetContainerReference(destinationContainerName);

    if (assetContainer.CreateIfNotExists())
    {
        assetContainer.SetPermissions(new BlobContainerPermissions
        {
            PublicAccess = BlobContainerPublicAccessType.Blob
        });
    }

    var sourceBlob = this.sourceContainer.GetBlockBlobReference(fileName);
    var assetFile = newAsset.AssetFiles.Create((sourceBlob as ICloudBlob).Name);

    this.CopyBlob(sourceBlob, assetContainer);

    sourceBlob.FetchAttributes();

    assetFile.ContentFileSize = (sourceBlob as ICloudBlob).Properties.Length;
    assetFile.Update();

    destinationLocator.Delete();
    writePolicy.Delete();

    sourceBlob.Delete();

    newAsset.Update();

    return newAsset.Id;
}
catch (Exception ex)
{
    this.logger.Error(string.Format(CultureInfo.InvariantCulture, ErrorMessages.EXCEPTION_ERROR, ex.StackTrace));
    return null;
}

}

`

`

private void CopyBlob(ICloudBlob sourceBlob, CloudBlobContainer destinationContainer)
{
var signature = sourceBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24)
});

CloudBlockBlob destinationBlob = destinationContainer.GetBlockBlobReference(sourceBlob.Name);

if (destinationBlob.Exists())
{
    Console.WriteLine(string.Format("Destination blob '{0}' already exists. Skipping.", destinationBlob.Uri));
}
else
{
    try
    {
        Console.WriteLine(string.Format("Copy blob '{0}' to '{1}'", sourceBlob.Uri, destinationBlob.Uri));

        using (Task task = destinationBlob.StartCopyAsync(new Uri(sourceBlob.Uri.AbsoluteUri + signature)))
        {
            task.Wait();
        }

        destinationBlob.FetchAttributes();

        while (destinationBlob.CopyState.Status == CopyStatus.Pending)
        {
            Thread.Sleep(50);
            destinationBlob.FetchAttributes();
        }

        if (destinationBlob.CopyState.Status != CopyStatus.Success)
        {
            throw new Exception("Copy failed: " + destinationBlob.CopyState.Status);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(string.Format("Error copying blob '{0}': {1}", sourceBlob.Name, ex.Message));
    }
}

}

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant