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

标签 c# azure asp.net-core azure-blob-storage

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

相关文章:

asp.net-core - 相对页面路径 'About' 只能在执行 Razor 页面时使用。指定一个

Azure Web 应用程序超时错误,响应为 500/502

c# - 使用虚拟 ListView 调用 BeginUpdate/EndUpdate 是否有用

c# - 在 ASP.Net 应用程序中添加数据集问题

c# - 当输入为 0 时停止取值

azure - azure 是否为每个 Web 应用程序分配不同的子网或 IP?

c# - 为正则表达式禁用全局

azure - 如何启用 Azure 防火墙策略的 TLS 检查和 IDPS 高级功能

azure - 将数据从 TFS 2015 导入到 Visual Studio Team Services?

azure - Azure 中的 301 重定向