我的服务非常简单,

public class AzureService : IAzureService
{
    private readonly CloudBlobContainer _container;
    public AzureService(ISettings settings)
    {
        var storageAccount = CloudStorageAccount.Parse(settings.BlobConnectionString);
        var blobClient = storageAccount.CreateCloudBlobClient();
        _container = blobClient.GetContainerReference(settings.BlobContainerName);
    }

    public Task UploadBlobAsync(string fileName, Stream stream)
    {
        var blob = _container.GetBlockBlobReference(fileName);
        return blob.UploadFromStreamAsync(stream);
    }

    public Task DeleteBlobAsync(string fileName)
    {
        var blob = _container.GetBlockBlobReference(fileName);
        return blob.DeleteAsync();
    }
}

这个方法是从
    public Task SaveAllAsync(Dictionary<string, Stream> images)
    {
        var tasks = new List<Task>();
        foreach (var image in images)
        {
            var fileName = image.Key;
            var stream = image.Value;
            var task = _azureService.UploadBlobAsync(fileName, stream);
            tasks.Add(task);
        }
        return Task.WhenAll(tasks);
    }

我的流是HttpPostedFileBase.InputStream。有时它可行,有时我得到The remote server returned an error: (400) Bad Request.。如果我设置一个断点,它也可以正常工作。

最佳答案

我遇到了同样的问题,我尝试一次上传20张图片,单线程作品,使用await Task.WhenAll的多线程失败,并显示“远程服务器返回错误:(400)错误的请求”。

  • 请参阅RequestInformation内的Microsoft.WindowsAzure.Storage.StorageException,这是从Upload [xxx] Async方法抛出的,以获取更多详细信息。
  • 首先,RequestInformation说了关于MD5问题的错误代码“Md5Mismatch”,买了我的直觉,否则,因为单线程像 super 按钮一样工作,然后..我发现了... DefaultRequestOptions.ParallelOperationThreadCount对象上的CloudBlobClient和问题已解决。
  • BlobRequestOptions Members MSDN

  •     private CloudBlobContainer ConnectToImageContainer()
        {
            var credentials = new StorageCredentials(AccountName, ImagesContainerKey);
            var account = new CloudStorageAccount(credentials, useHttps: true);
            var client = account.CreateCloudBlobClient();
            client.DefaultRequestOptions.ParallelOperationThreadCount = 64; // max value
            client.DefaultRequestOptions.SingleBlobUploadThresholdInBytes = 67108864; // max value
            var container = client.GetContainerReference(ImagesContainerName);
            return container;
        }
    

    09-28 03:01
    查看更多