繁体   English   中英

Azure Worker角色和Blob存储C#-Microsoft.WindowsAzure.Storage.StorageException:远程服务器返回错误:(400)错误的请求

[英]Azure Worker role and blob storage c# - Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (400) Bad Request

我已将文件上传到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](RESTCommand 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 C:\\ Program Files(x86)\\ Jenkins \\ workspace \\ release_dotnet_master \\ Lib \\ ClassLibraryCommon \\ Core \\ Executor \\ Executor.cs:行604中的cmd,IRetryPolicy策略,OperationContext operationContext):Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.CreateIfNotExists (C:\\ Program Files(x86)\\ Jenkins \\ workspace \\ release_dotnet_master \\ Lib \\ ClassLibraryCommon \\ Blob \\ CloudBlobContainer.cs:Line 199中的BlobContainerPublicAccessType accessType,BlobRequestOptions requestOptions,OperationContext operationContext):WorkerRoleWithSBQueue1.WorkerRole.bred_4_0上的199行 receiveMessage)

任何帮助将不胜感激。

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

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/zh-cn/library/azure/dd135715.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM