我有一个非常简单的服务,
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.有时它工作,有时我得到远程服务器返回错误:(400)错误的请求..如果我放一个断点它也有效.
解决方法
我有同样的问题,我尝试上传20个图像,1次攻击,单线程工作,多线程使用await Task.WhenAll失败,“远程服务器返回错误:(400)错误请求.”
>请参阅上传[xxx]异步方法抛出的Microsoft.WindowsAzure.Storage.StorageException中的RequestInformation以获取更多详细信息.
>首先,RequestInformation说了一个关于MD5问题的错误代码“Md5Mismatch”,买了我的直觉说不然因为单线程就像一个魅力,然后……我发现它… DefaultBlobClient上的DefaultRequestOptions.ParallelOperationThreadCount对象和问题sovled.
> 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; }