Pin*_*ave 1 .net asp.net-mvc azure azure-storage asp.net-core
我正在使用 ASP.NET Core 2.1 Azure Cloud Storage 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");
}
Run Code Online (Sandbox Code Playgroud)
我已经在我这边进行了测试。这是真的,您需要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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1001 次 |
| 最近记录: |