【问题标题】:Azure Worker role and blob storage c# - Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (400) Bad RequestAzure Worker 角色和 blob 存储 c# - Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (400) Bad Request
【发布时间】:2017-02-20 08:02:39
【问题描述】:

我已在 Blob 存储中上传文件。我正在尝试从工作角色下载这些文件以对其进行一些处理。容器名称从 WebApi2 发送到队列。

worker 角色首先从队列中获取容器名称,然后尝试下载该容器中的 blob。

以下是名称的代码:

  public override void Run()
    {
        Trace.WriteLine("Starting processing of messages");

        // Initiates the message pump and callback is invoked for each message that is received, calling close on the client will stop the pump.
        Client.OnMessage((receivedMessage) =>
        {
            try
            {
                // Process the message
                Trace.WriteLine("Processing Service Bus message: " + receivedMessage.SequenceNumber.ToString());
                string msg = "Container Name: " + receivedMessage.GetBody<String>();
                Trace.WriteLine("Processing Service Bus message: " + msg);

                CloudStorageAccount storageAccount =   CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("MyStorage"));


                CloudBlobContainer imagesContainer = null;


                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                imagesContainer = blobClient.GetContainerReference(msg);


                // Create the container if it doesn't already exist.
                imagesContainer.CreateIfNotExists();


                imagesContainer.SetPermissions(new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });

                var blobs = imagesContainer.ListBlobs();
                var listOfFileNames = new List<string>();


                foreach (var blob in blobs)
                {
                    var blobFileName = blob.Uri.Segments.Last();
                    listOfFileNames.Add(blobFileName);
                    Trace.WriteLine(listOfFileNames);
                }

                if (listOfFileNames == null)
                {

                    Trace.WriteLine("present");
                }



                for (i = 1; i < 3; i++)
                {
                    CloudBlockBlob signBlob =    imagesContainer.GetBlockBlobReference(i + ".txt");

                    MemoryStream lms = new MemoryStream();
                    signBlob.DownloadToStream(lms);
                    lms.Seek(0, SeekOrigin.Begin);

                    StreamReader SR = new StreamReader(lms);
                    Trace.WriteLine(SR);
                }


            }




            catch(Microsoft.WindowsAzure.Storage.StorageException e)
            {
                // Handle any message processing specific exceptions here
                Trace.WriteLine("Error:" + e);
            }
        });

        CompletedEvent.WaitOne();
    }

我收到以下异常:

enter code hereException thrown: 'Microsoft.WindowsAzure.Storage.StorageException' in Microsoft.WindowsAzure.Storage.dll

错误:Microsoft.WindowsAzure.Storage.StorageException:远程服务器返回错误:(400) 错误请求。 ---> System.Net.WebException:远程服务器返回错误:(400)错误请求。 在 System.Net.HttpWebRequest.GetResponse() 在 Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 677 --- End of inner exception stack trace --- at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand1 cmd, IRetryPolicy policy, OperationContext operationContext) 在 c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon \Core\Executor\Executor.cs:第 604 行 在 Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.CreateIfNotExists(BlobContainerPublicAccessType accessType, BlobRequestOptions requestOptions, OperationContext operationContext) 在 c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlobContainer.cs:line 199 在 WorkerRoleWithSBQueue1.WorkerRole.b__4_0(BrokeredMessage receivedMessage)

任何帮助将不胜感激。

【问题讨论】:

  • 您要创建的容器的名称是什么?创建容器时的 400 错误通常表示容器名称无效。
  • 你能通过公共 http url 访问容器中的任何 blob 吗?

标签: c# azure azure-worker-roles azure-blob-storage


【解决方案1】:

查看您的代码,您正在执行以下操作:

string msg = "Container Name: " + receivedMessage.GetBody<String>();

然后您正在执行以下操作:

        imagesContainer = blobClient.GetContainerReference(msg);
        // Create the container if it doesn't already exist.
        imagesContainer.CreateIfNotExists();

所以基本上你正在创建一个以Container Name 开头的容器名称,这是容器名称的无效值。这就是您收到错误的原因。

有关 blob 容器的有效命名约定,请参阅此链接:https://msdn.microsoft.com/en-us/library/azure/dd135715.aspx

【讨论】:

  • 是的,先生。代码将容器名称视为“容器名称:”+ msg,这是不正确的。容器名称只是字符串“msg”中的值。
  • 不客气。不确定您是否看过我关于帐户密钥的评论(我标记了您包含帐户密钥的评论,但它已被删除)。如果您还没有这样做,请尽快更改您的帐户密钥。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-26
  • 2013-01-29
  • 2019-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-26
相关资源
最近更新 更多