martoo
martoo

Reputation: 103

Uploading .mp4 to Azure Media Service from web api C#

I have an API written in C# that is meant to recieve a file from frontend. As of now it's a byte array and i want to convert this to a .mp4 file and then send it to my azure media service with the blobstorage. I do not want to store it locally and i can't read it from disk either. What is the best approach for this?

I create my CloudBlobClient like so:

 private CloudBlobClient CloudBlobClient()
        {
            var storageAccount =         CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString);
            var blobStorage = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobStorage.GetContainerReference(Constants.VideoBlobContainer);
            if (container.CreateIfNotExist())
            {
                var permissions = container.GetPermissions();
                permissions.PublicAccess = BlobContainerPublicAccessType.Container;
                container.SetPermissions(permissions);
            }
            return blobStorage;
        }

Then I have this method that i've started

 private Uri UploadToStorage(CloudBlobClient blobStorage, byte[] video, VideoSize size)
        {
            var uniqueBlobName = GetVideoUriAsString(VideoId, Type, size);
            CloudBlockBlob blob = blobStorage.GetBlockBlobReference(uniqueBlobName);

I'm not sure how to proceede here. I have been looking around a lot on the web for approaches but all I find is example of console applications reading from disk.

Is there anyone familliar with this type of uploading to media serivces?

Upvotes: 1

Views: 948

Answers (1)

adaam
adaam

Reputation: 3706

You're on your way there, although you should just obtain the reference to the blob from the blob container from the first method. Very rough but here you go:

public void uploadBytesToBlobWithMimeAndStorageCreds(string theFolder, string theFileName, byte[] videoBytes, string theMimeType)
{
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString);

    CloudBlobClient client = storageAccount.CreateCloudBlobClient;
    CloudBlobContainer container = client.GetContainerReference(theFolder);

    CloudBlob blob = container.GetBlobReference(theFileName);
    blob.UploadByteArray(theBytes);
    blob.Properties.CacheControl = "max-age=3600, must-revalidate";
    blob.Properties.ContentType = theMimeType; // e.g. "video/mp4"
    blob.SetProperties();

}

Upvotes: 1

Related Questions