简体   繁体   English

Azure Cloud Storage SDK UploadFromStreamAsync存储0字节

[英]Azure Cloud Storage SDK UploadFromStreamAsync storing 0 bytes

I am using ASP.NET Core 2.1 Azure Cloud Storage SDK UploadFromStreamAsync method to upload stream as azure blob. 我正在使用ASP.NET Core 2.1 Azure云存储SDK UploadFromStreamAsync方法将流上传为天蓝色的blob。 The blob is created but size shows 0 bytes. 已创建Blob,但大小显示0个字节。 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");
        }

I have tested it on my side. 我已经对它进行了测试。 And that is true you need to set the stream position to 0 before uploading. 确实如此,您需要在上传之前将流位置设置为0 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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM