gpt4 book ai didi

c# - Azure 云存储 SDK UploadFromStreamAsync 不起作用

转载 作者:太空狗 更新时间:2023-10-29 22:01:57 27 4
gpt4 key购买 nike

我正在尝试将文件上传到 .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
}
}

代码执行时出现以下错误-

{System.ObjectDisposedException: Cannot access a closed file. at System.IO.FileStream.get_Position() at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.get_Position() at Microsoft.AspNetCore.Http.Internal.ReferenceReadStream.VerifyPosition() at Microsoft.AspNetCore.Http.Internal.ReferenceReadStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) at Microsoft.WindowsAzure.Storage.Core.Util.StreamExtensions.WriteToAsync[T](Stream stream, Stream toStream, IBufferManager bufferManager, Nullable1
copyLength, Nullable
1 maxLength, Boolean calculateMd5, ExecutionState1 executionState, StreamDescriptor streamCopyState,
CancellationToken token) in C:\Program Files
(x86)\Jenkins\workspace\release_dotnet_master\Lib\Common\Core\Util\StreamExtensions.cs:line
301 at
Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStreamAsyncHelper(Stream
source, Nullable
1 length, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext, IProgress1 progressHandler, CancellationToken cancellationToken) in
C:\Program Files
(x86)\Jenkins\workspace\release_dotnet_master\Lib\WindowsRuntime\Blob\CloudBlockBlob.cs:line
352 at
Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStreamAsyncHelper(Stream
source, Nullable
1 length, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken) in C:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\WindowsRuntime\Blob\CloudBlockBlob.cs:line 290 at Common.AzureCloudStorage.UploadContent(CloudBlobContainer containerReference, Stream contentStream, String blobName)

对我有用的替代解决方案: adding to azure blob storage with stream

请问有什么帮助吗?如果我可以提供更多详细信息,请告诉我。

最佳答案

我的解决方案是等到任务完成后再继续,例如

    private async void SaveAsync(IFormFile file)
{
CloudBlockBlob blob = this.blobContainer.GetBlockBlobReference(file.FileName);
var task = blob.UploadFromStreamAsync(file.OpenReadStream(), file.Length);

while (task.IsCompleted == false) {
Thread.Sleep(1000);
}

}

也许传递长度也有帮助?

关于c# - Azure 云存储 SDK UploadFromStreamAsync 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51233494/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com