1

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?

Shaeldon
  • 873
  • 4
  • 18
  • 28
martoo
  • 103
  • 1
  • 9

1 Answers1

1

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();

}
adaam
  • 3,700
  • 7
  • 27
  • 51
  • This worked perfectly, Thank you adaam. If i want to create a thumbnail for this video, what would be the best approach? Thanks – martoo Mar 17 '15 at 15:08
  • @martoo I haven't ever done that, but it looks relatively easy to do - see http://stackoverflow.com/questions/15702031/get-thumbnail-image-of-video-file-in-c-sharp – adaam Mar 17 '15 at 16:35
  • I have another question for you adaam if you have the time, If i want to copy the video from the blob storage into a media service, what would be the best approach, this is what i have so far: See Edit – martoo Mar 17 '15 at 18:04
  • your answer got overwritten there sorry for that adaam. `public void UploadToMediaServices(Uri storageAddress) { var filePath = storageAddress.ToString(); var context = new CloudMediaContext("Name", "Key"); var uploadAsset = context.Assets.Create(Path.GetFileNameWithoutExtension(filePath), AssetCreationOptions.None); var assetFile = uploadAsset.AssetFiles.Create(Path.GetFileName(filePath)); assetFile.Upload(filePath); }` – martoo Mar 17 '15 at 18:21
  • @martoo See the method `CreateAssetFromExistingBlobs` in the code example on this page: https://msdn.microsoft.com/en-us/library/azure/jj933290.aspx – adaam Mar 17 '15 at 23:07