我試圖創建一個實用程序從互聯網上下載文件並將其重新上傳到Azure blob存儲。 Blob容器已經創建好;但由於某種原因,當我嘗試將文件上傳到存儲器時,出現「不良請求400」異常...創建容器名稱,小寫字母,特殊字符。但我仍然不知道爲什麼我會得到例外!無法上傳到azure Blob存儲:遠程服務器返回錯誤:(400)錯誤的請求
請幫忙。
注意:
- 我不使用任何模擬器...雲上的直接測試。
- 所有帶「公共容器」訪問選項的容器。
這裏是個例外:
An exception of type 'Microsoft.WindowsAzure.Storage.StorageException'
occurred in Microsoft.WindowsAzure.Storage.dll but was not handled in user code
Additional information: The remote server returned an error: (400) Bad Request.
這裏是代碼:
foreach (var obj in objectsList)
{
var containerName = obj.id.Replace("\"", "").Replace("_", "").Trim();
CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);
if (blobContainer.Exists())
{
var fileNamesArr = obj.fileNames.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var sora in fileNamesArr)
{
int soraInt = int.Parse(sora.Replace("\"", ""));
String fileName = String.Format("{0}.mp3", soraInt.ToString("000"));
var url = String.Format("http://{0}/{1}/{2}", obj.hostName.Replace("\"", ""), obj.id.Replace("\"", ""), fileName.Replace("\"", "")).ToLower();
var tempFileName = "temp.mp3";
var downloadedFilePath = Path.Combine(Path.GetTempPath(), tempFileName).ToLower();
var webUtil = new WebUtils(url);
await webUtil.DownloadAsync(url, downloadedFilePath).ContinueWith(task =>
{
var blobRef = blobContainer.GetBlockBlobReference(fileName.ToLower());
blobRef.Properties.ContentType = GetMimeType(downloadedFilePath);
using (var fs = new FileStream(downloadedFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
blobRef.UploadFromStream(fs); // <--- Exception
}
});
}
}
else
{
throw new Exception(obj.id.Replace("\"", "") + " Container not exist!");
}
}
編輯:存儲異常
Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (400) Bad Request. ---> System.Net.WebException: The remote server returned an error: (400) Bad Request. at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream() at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand
1 cmd, IRetryPolicy policy, OperationContext operationContext) --- End of inner exception stack trace --- at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand
1 cmd, IRetryPolicy policy, OperationContext operationContext) at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStreamHelper(Stream source, Nullable`1 length, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext) at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromStream(Stream source, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext) at TelawatAzureUtility.StorageService.<>c__DisplayClass4.b__12(Task task) in \psf\Home\Documents\Visual Studio 14\Projects\Telawat Azure Utility\TelawatAzureUtility\StorageService.cs:line 128 Request Information RequestID: RequestDate:Sat, 28 Jun 2014 20:12:14 GMT StatusMessage:Bad Request
編輯2:請求信息:
編輯3:問題來自WebUtils ..我用下面的代碼替換它和它的作品!我將添加weUtils代碼,也許你可以幫助知道它有什麼問題。
HttpClient client = new HttpClient();
var stream = await client.GetStreamAsync(url);
WebUtils代碼:
public class WebUtils
{
private Lazy<IWebProxy> proxy;
public WebUtils(String url)
{
proxy = new Lazy<IWebProxy>(() => string.IsNullOrEmpty(url) ? null : new WebProxy {
Address = new Uri(url), UseDefaultCredentials = true });
}
public IWebProxy Proxy
{
get { return proxy.Value; }
}
public Task DownloadAsync(string requestUri, string filename)
{
if (requestUri == null)
throw new ArgumentNullException("requestUri is missing!");
return DownloadAsync(new Uri(requestUri), filename);
}
public async Task DownloadAsync(Uri requestUri, string filename)
{
if (filename == null)
throw new ArgumentNullException("filename is missing!");
if (Proxy != null)
{
WebRequest.DefaultWebProxy = Proxy;
}
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri))
{
using (Stream contentStream = await (await httpClient.SendAsync(request)).Content.ReadAsStreamAsync())
{
using (var stream = new FileStream(filename, FileMode.Create, FileAccess.Write))
{
contentStream.CopyTo(stream);
stream.Flush();
stream.Close();
}
contentStream.Close();
}
}
}
}
}
此外,當我嘗試這樣的代碼......在 '等待' 將永遠不會完成或已完成!
webUtil.DownloadAsync(url, downloadedFilePath).Wait()
您正在使用哪種版本的存儲客戶端庫?你能通過Fiddler追蹤請求/響應嗎?這應該會給你一些關於400錯誤的更多細節。 –
來自Nuget: ...我現在就去檢查一下提琴手。 –
bunjeeb
請使用Fiddler運行您的實用程序,以便您可以捕獲請求/響應並在此處共享它們。 –