[英]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. 我已将文件上传到Blob存储中。 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.
容器名称从WebApi2发送到队列。
The worker role first fetches the container name from the queue and then tries to download the blobs within that container. worker角色首先从队列中获取容器名称,然后尝试下载该容器中的blob。
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. 错误:Microsoft.WindowsAzure.Storage.StorageException:远程服务器返回错误:(400)错误的请求。 ---> System.Net.WebException: The remote server returned an error: (400) Bad Request.
---> System.Net.WebException:远程服务器返回错误:(400)错误的请求。 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 在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行 receivedMessage) receiveMessage)
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. 因此,基本上,您正在创建一个容器名称,该名称以“容器名称”开头,这是该
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 . 请参阅此链接以获取Blob容器的有效命名约定: https : //msdn.microsoft.com/zh-cn/library/azure/dd135715.aspx 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.