我正在尝试将文件上传到.Net Core 2.1中的Azure blob存储。下面是我的代码。
IFormFileCollection files = formCollection.Files;
foreach (var file in files)
{
if (file.Length > 0)
{
_azureCloudStorage.UploadContent(cloudBlobContainer, file.OpenReadStream(), file.FileName);
}
}
UploadContent
的实现-public async void UploadContent(CloudBlobContainer containerReference, Stream contentStream, string blobName)
{
try
{
using (contentStream)
{
var blockBlobRef = containerReference.GetBlockBlobReference(blobName);
//await containerReference.SetPermissionsAsync(new BlobContainerPermissions
//{
// PublicAccess = BlobContainerPublicAccessType.Blob
//});
await blockBlobRef.UploadFromStreamAsync(contentStream);
}
}
catch(Exception ex)
{
//Error here
}
}
代码执行时出现以下错误-
对我有用的替代解决方案:adding to azure blob storage with stream
请帮忙吗?如果可以提供更多详细信息,请告诉我。
最佳答案
我的解决方案是包括文件长度并设置位置= 0。
MemoryStream outStr = new MemoryStream();
CloudBlockBlob myBlob = container.GetBlockBlobReference(name);
outStr.Position = 0;
await myBlob.UploadFromStreamAsync(outStr, outStr.Length);