Azure辅助角色和blob存储c# - Microsoft.WindowsAzure.Storage.StorageException:远程服务器返回错误:(400)错误请求

时间:2016-10-11 17:20:46

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

我已经在blob存储中上传了文件。我试图从worker角色下载这些文件来对其进行一些处理。容器名称从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)错误请求。 ---&GT; System.Net.WebException:远程服务器返回错误:(400)错误请求。    在System.Net.HttpWebRequest.GetResponse()    在Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync [T](REST命令1 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](RESTCommand 1 cmd,IRetryPolicy策略,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:第199行    在WorkerRoleWithSBQueue1.WorkerRole.b__4_0(BrokeredMessage receivedMessage)

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分: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