使用流添加到 azure blob 存储
adding to azure blob storage with stream
我正在尝试将通过 .net 核心网络 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: 'Cannot access a closed Stream.'
Inner Exception
ObjectDisposedException: Cannot access a closed Stream.
调试时,我什至在 fileStream.Position = 0
之前就注意到它的位置已经是 0。但是我添加了这一行,因为我收到了这个错误。同样在 await 行,fileStream
的 _disposed
设置为 false。
此外,关于 blob 连接,我尝试为字符串常量 AccessKey
设置无效值,但它显示完全相同的错误。这意味着我不知道它是否是偶数连接。我已经在调试器中检查了 blobBlock
内的所有值,但我不知道如何验证它是否已连接。
尝试直接从流写入时似乎出现了一些问题。通过将流转换为字节数组,我能够 运行 代码。
await blockBlob.UploadFromByteArrayAsync(ReadFully(fileStream, blockBlob.StreamWriteSizeInBytes),
0, (int)fileStream.Length);
ReadFully
是对这个答案的修改
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();
}
}
我正在尝试将通过 .net 核心网络 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: 'Cannot access a closed Stream.'
Inner Exception ObjectDisposedException: Cannot access a closed Stream.
调试时,我什至在 fileStream.Position = 0
之前就注意到它的位置已经是 0。但是我添加了这一行,因为我收到了这个错误。同样在 await 行,fileStream
的 _disposed
设置为 false。
此外,关于 blob 连接,我尝试为字符串常量 AccessKey
设置无效值,但它显示完全相同的错误。这意味着我不知道它是否是偶数连接。我已经在调试器中检查了 blobBlock
内的所有值,但我不知道如何验证它是否已连接。
尝试直接从流写入时似乎出现了一些问题。通过将流转换为字节数组,我能够 运行 代码。
await blockBlob.UploadFromByteArrayAsync(ReadFully(fileStream, blockBlob.StreamWriteSizeInBytes),
0, (int)fileStream.Length);
ReadFully
是对这个答案的修改
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();
}
}