【问题标题】:Azure Cloud Storage SDK UploadFromStreamAsync storing 0 bytesAzure 云存储 SDK UploadFromStreamAsync 存储 0 个字节
【发布时间】:2019-10-02 21:32:30
【问题描述】:

我正在使用 ASP.NET Core 2.1 Azure 云存储 SDK UploadFromStreamAsync 方法将流上传为 azure blob。 Blob 已创建,但大小显示为 0 字节。下面是我的代码。任何人都可以告诉我我是否遗漏了什么?

   [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");
        }

【问题讨论】:

  • 尝试在上传之前将流的位置重置为0。更多详情请看这里:stackoverflow.com/questions/34474850/….
  • 我也试过了:stream.Seek(0, SeekOrigin.Begin);但结果还是一样。
  • 流的长度是多少?
  • 流超过 4 MB,但我认为这不是问题,因为我没有收到任何与流长度相关的错误。我仍然使用以下stackoverflow帖子中建议的方法增加了控制器操作( [RequestSizeLimit(100_000_000)] )的大小:stackoverflow.com/questions/38698350/…。但行为仍然没有改变

标签: .net asp.net-mvc azure asp.net-core azure-storage


【解决方案1】:

我已经对它进行了测试。这是真的,您需要在上传之前将流位置设置为0。 下面是我的示例代码,效果很好。

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

【讨论】:

  • 在我的例子中,代码在面向 .NET 4.6.1 时运行良好。然后,在将其更改为目标 .NET Standard 后,我必须将 Stream 位置设置回 0,否则不会上传任何数据。
猜你喜欢
  • 2018-12-16
  • 2020-01-20
  • 2020-09-22
  • 2014-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多