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");
}
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.