【发布时间】:2017-02-20 08:02:39
【问题描述】:
我已在 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](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](RESTCommand1 cmd, IRetryPolicy policy, 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:line 199
在 WorkerRoleWithSBQueue1.WorkerRole.b__4_0(BrokeredMessage receivedMessage)
任何帮助将不胜感激。
【问题讨论】:
-
您要创建的容器的名称是什么?创建容器时的 400 错误通常表示容器名称无效。
-
你能通过公共 http url 访问容器中的任何 blob 吗?
标签: c# azure azure-worker-roles azure-blob-storage