2

I am using ASP.NET Core 2.1 Azure Cloud Storage SDK UploadFromStreamAsync method to upload stream as azure blob. The blob is created but size shows 0 bytes. Below is my code. Anyone can tell me if I am missing something ?

   [HttpPost]
        public async Task<IActionResult> PostRecordedAudioVideo()
        {
            var file = Request.Form.Files[0];

            if (file.Length > 0)
            {
                var stream = new MemoryStream();

                await this.Request.Body.CopyToAsync(stream);

                CloudStorageAccount storageAccount = null;
                CloudBlobContainer cloudBlobContainer = null;

                string storageConnectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");

                if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
                {   
                        CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
                        cloudBlobContainer = cloudBlobClient.GetContainerReference("screening-videos");
                        CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("TestBlobName");
                        await cloudBlockBlob.UploadFromStreamAsync(stream);
                }
            }
            return Json("Success or failure response");
        }
Pinal Dave
  • 533
  • 4
  • 14
  • 27
  • 1
    Try by resetting the stream's position to `0` before uploading. Please see here for more details: https://stackoverflow.com/questions/34474850/is-stream-reading-can-make-and-send-null-to-blob-storage/34489697#34489697. – Gaurav Mantri May 15 '19 at 17:17
  • I tried that too : stream.Seek(0, SeekOrigin.Begin); But still same outcome. – Pinal Dave May 15 '19 at 18:42
  • What's the stream length? – Gaurav Mantri May 15 '19 at 18:44
  • stream is more than 4 MB, but I think that's not a problem as I am not getting any stream length related error. Still I increased size at controller action ( [RequestSizeLimit(100_000_000)] ) using the method suggested in below stackoverflow post : https://stackoverflow.com/questions/38698350/increase-upload-file-size-in-asp-net-core . But still no change in bahavior – Pinal Dave May 15 '19 at 19:38

1 Answers1

7

I have tested it on my side. And that is true you need to set the stream position to 0 before uploading. Below is my sample code which works well.

 static void Main(string[] args)
    {

        var stream = new MemoryStream();
        var sw = new StreamWriter(stream);
        sw.Write("adiojaihdhwjfoewjfioghauhfjowpf");
        sw.Flush();

        stream.Position = 0;

        UploadfromstreamAsync(stream).Wait();
    }

    static async System.Threading.Tasks.Task UploadfromstreamAsync(MemoryStream stream)
    {
        CloudStorageAccount storageAccount = null;
        CloudBlobContainer cloudBlobContainer = null;

        string storageConnectionString = "connectionstring";

        if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
        {
            CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
            cloudBlobContainer = cloudBlobClient.GetContainerReference("123");
            CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("TestBlobName2");
            await cloudBlockBlob.UploadFromStreamAsync(stream);
        }
    }
Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
  • 1
    In my case, code was working fine when targeting .NET 4.6.1. Then, after changing it to target .NET Standard, I had to set the Stream position back to 0, otherwise no data was uploaded. – Igor Kondrasovas Feb 06 '20 at 09:49