我正在尝试添加一个IFormFile
通过 .net core Web API 接收到 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:“无法访问关闭的流。”
内部异常
ObjectDisposeException:无法访问关闭的流。
调试时,我什至之前就注意到了fileStream.Position = 0
它的位置已经是 0。但是我添加了该行,因为我收到此错误。就在等候线处,fileStream
's _disposed
设置为 false。
此外,关于 blob 连接,我尝试为字符串常量设置无效值AccessKey
它显示完全相同的错误。这意味着我不知道它是否是偶数连接。我已经检查了其中的所有值blobBlock
在调试器中,但我不知道如何验证它是否已连接。
尝试直接从流写入时似乎存在一些问题。我能够通过将流转换为字节数组来运行代码。
await blockBlob.UploadFromByteArrayAsync(ReadFully(fileStream, blockBlob.StreamWriteSizeInBytes),
0, (int)fileStream.Length);
The ReadFully
是对这个答案的修改https://stackoverflow.com/a/221941 https://stackoverflow.com/a/221941
static byte[] ReadFully(Stream input, int size)
{
byte[] buffer = new byte[size];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, size)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)