【发布时间】:2017-11-14 22:06:49
【问题描述】:
我正在尝试将通过 .net 核心 Web API 接收的 IFormFile 添加到 azure blob 存储。这些是我设置的属性:
static internal CloudStorageAccount StorageAccount =>
new CloudStorageAccount(new StorageCredentials(AccountName, AccessKey, AccessKeyName), true);
// Create a blob client.
static internal CloudBlobClient BlobClient => StorageAccount.CreateCloudBlobClient();
// Get a reference to a container
static internal CloudBlobContainer Container(string ContainerName)
=> BlobClient.GetContainerReference(ContainerName);
static internal CloudBlobContainer ProfilePicContainer
=> Container(ProfilePicContainerName);
现在我像这样使用ProfilePicContainer:
var Container = BlobStorage.ProfilePicContainer;
string fileName = Guid.NewGuid().ToString("N") + Path.GetExtension(ProfileImage.FileName);
var blockBlob = Container.GetBlockBlobReference(fileName);
var fileStream = ProfileImage.OpenReadStream();
fileStream.Position = 0;
await blockBlob.UploadFromStreamAsync(fileStream);
这给了我以下错误:
Microsoft.WindowsAzure.Storage.StorageException: '无法访问已关闭的流。'
内部异常 ObjectDisposedException:无法访问已关闭的 Stream。
在调试时,我甚至在fileStream.Position = 0 之前就注意到它的位置已经是 0。但是我添加了该行,因为我收到了这个错误。同样在 await 行,fileStream 的 _disposed 设置为 false。
此外,关于 blob 连接,我尝试为字符串常量 AccessKey 设置无效值,它显示完全相同的错误。这意味着我不知道它是否甚至是连接。我已经在调试器中检查了blobBlock 中的所有值,但我不知道如何验证它是否已连接。
【问题讨论】:
-
听起来
fileStream已经关闭而不是azure storage sdk 的问题?您可以访问fileStream流吗?你能用using (var testStream = File.OpenWrite(Path.GetTempFileName())) fileStream.CopyTo(testStream)之类的东西把它写到磁盘吗? -
是的,已经尝试使用 fileStream 写入文件和写入字节。两者都工作正常。
-
尝试将流放在 using 块中
-
我之前尝试过。我有其他错误,并在网上看到建议不要使用 using
标签: c# asynchronous azure-storage filestream asp.net-core-webapi