alan samuel
alan samuel

Reputation: 425

How to download a video from Azure Blob Storage?

I am trying to download a video from Azure blob storage like this. But every time I download it, the file is always empty. I tested it out by uploading the same file into the storage and checking it. When I check the size of the blob file, its empty in Azure storage.

        //Gets the folder and the particular video to upload to YouTube
        CloudBlobContainer videoscontainer = blobClient.GetContainerReference("videos");
        CloudBlob videofile = videoscontainer.GetBlobReference("test.mp4");

        MemoryStream videofileinmemory = new MemoryStream();
        await videofile.DownloadToStreamAsync(videofileinmemory);


       CloudBlobContainer container = blobClient.GetContainerReference("checkedvideos");
        //Create the container if it doesn't already exist.
        container.CreateIfNotExists();
        container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

        CloudBlockBlob blockBlob = container.GetBlockBlobReference("sametestfile.mp4");
        blockBlob.UploadFromStream(videofileinmemory);

Upvotes: 1

Views: 2013

Answers (1)

Tom Sun
Tom Sun

Reputation: 24549

On the azure blob storage, there is no file type. So there is no difference to download .mp4 blob from other blob.

In your case, you need to reset the videofileinmemory Position = 0 before upload the blob. Then it should work.

videofileinmemory.Position = 0;
blockBlob.UploadFromStream(videofileinmemory);

If we want to copy blob from one container to another, there is no need to download the blob to memory. We also could use CloudBlockBlob.StartCopy.

 CloudBlockBlob videofile = videoscontainer.GetBlockBlobReference("test.mp4");
 CloudBlockBlob blockBlob = container.GetBlockBlobReference("sametestfile.mp4");
 blockBlob.StartCopy(videofile);

Upvotes: 1

Related Questions