gpt4 book ai didi

.net - Azure 云存储 SDK UploadFromStreamAsync 存储 0 字节

转载 作者:行者123 更新时间:2023-12-01 21:57:00 25 4
gpt4 key购买 nike

我正在使用 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。下面是我的示例代码,效果很好。

 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 - Azure 云存储 SDK UploadFromStreamAsync 存储 0 字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56154420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com