2014-06-28 50 views
8

我试图创建一个实用程序从互联网上下载文件并将其重新上传到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:请求信息:

enter image description here

enter image description here

编辑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() 
+0

您正在使用哪种版本的存储客户端库?你能通过Fiddler追踪请求/响应吗?这应该会给你一些关于400错误的更多细节。 –

+0

来自Nuget: ...我现在就去检查一下提琴手。 – bunjeeb

+0

请使用Fiddler运行您的实用程序,以便您可以捕获请求/响应并在此处共享它们。 –

回答

20

您是否尝试过在Azure门户上手动创建容器?它对可以给容器的名称有一些限制。

例如:容器名称不能包含大写字母。

如果您请求一个名称无效的容器,将导致(400)您收到的错误请求。所以检查你的“containerName”字符串。

+3

这帮了我。在我的容器名称开始时,我只有一个大写字母,在Azure中全是小写字母。 – user1352057

1

我有一个非常不同的错误请求消息的情况。张贴在这里可能会碰到同样的其他人。就我而言,我只是围绕其他资源组移动资源。在那次洗牌中,天蓝色的臭虫让我将存储设备指向我所在地区不可用的位置(“东南亚”)。因此,针对存储帐户的所有请求都返回了错误的请求消息。我花了一段时间才弄清楚,因为我创建了另一个存储帐户进行测试,创建时Azure不允许我选择“东南亚”作为选择的位置,所以我选择了另一个位置(“东亚“),然后一切正常。

0

我也遇到了Azure存储消息队列的错误。

Azure存储消息队列名称也必须全部小写。 ie:小写的“newqueueitem”名称。

// Retrieve a reference to a queue. 
CloudQueue queue = queueClient.GetQueueReference("newqueueitem"); 

// Create the queue if it doesn't already exist. 
queue.CreateIfNotExists(); 
相关问题