简体   繁体   中英

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

I have uploaded files in blob storage. I am trying to download those files from worker role to do some processing on it. The container name is sent from the WebApi2 to the queue.

The worker role first fetches the container name from the queue and then tries to download the blobs within that container.

Below is the code for the name:

  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();
    }

I am getting the below Exception:

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

Error: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.GetResponse() at 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 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\\Program Files (x86)\\Jenkins\\workspace\\release_dotnet_master\\Lib\\ClassLibraryCommon\\Core\\Executor\\Executor.cs:line 604 at Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.CreateIfNotExists(BlobContainerPublicAccessType accessType, BlobRequestOptions requestOptions, OperationContext operationContext) in c:\\Program Files (x86)\\Jenkins\\workspace\\release_dotnet_master\\Lib\\ClassLibraryCommon\\Blob\\CloudBlobContainer.cs:line 199 at WorkerRoleWithSBQueue1.WorkerRole.b__4_0(BrokeredMessage receivedMessage)

Any help would be highly appreciated.

Looking at your code, you're doing the following:

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

And then you're doing the following:

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

So basically you're creating a container name of which starts with Container Name which is an invalid value for the container name. This is why you're getting the error.

Please see this link for valid naming convention for blob container: https://msdn.microsoft.com/en-us/library/azure/dd135715.aspx .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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