Azure Worker role and blob storage c# - Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (400) Bad Request
Azure Worker role and blob storage c# - Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (400) Bad Request
我已将文件上传到 blob 存储中。我正在尝试从工作者角色下载这些文件以对其进行一些处理。容器名称从 WebApi2 发送到队列。
工作者角色首先从队列中获取容器名称,然后尝试下载该容器中的 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](RESTCommand
1 cmd,IRetryPolicy 策略,OperationContext operationContext) 在 c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 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)
非常感谢任何帮助。
查看您的代码,您正在执行以下操作:
string msg = "Container Name: " + receivedMessage.GetBody<String>();
然后您将执行以下操作:
imagesContainer = blobClient.GetContainerReference(msg);
// Create the container if it doesn't already exist.
imagesContainer.CreateIfNotExists();
所以基本上您正在创建一个以 Container Name
开头的容器名称,这是容器名称的无效值。这就是您收到错误的原因。
请参阅此 link 以了解 blob 容器的有效命名约定:https://msdn.microsoft.com/en-us/library/azure/dd135715.aspx。
我已将文件上传到 blob 存储中。我正在尝试从工作者角色下载这些文件以对其进行一些处理。容器名称从 WebApi2 发送到队列。
工作者角色首先从队列中获取容器名称,然后尝试下载该容器中的 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](RESTCommand
1 cmd,IRetryPolicy 策略,OperationContext operationContext) 在 c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 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)
非常感谢任何帮助。
查看您的代码,您正在执行以下操作:
string msg = "Container Name: " + receivedMessage.GetBody<String>();
然后您将执行以下操作:
imagesContainer = blobClient.GetContainerReference(msg);
// Create the container if it doesn't already exist.
imagesContainer.CreateIfNotExists();
所以基本上您正在创建一个以 Container Name
开头的容器名称,这是容器名称的无效值。这就是您收到错误的原因。
请参阅此 link 以了解 blob 容器的有效命名约定:https://msdn.microsoft.com/en-us/library/azure/dd135715.aspx。