gpt4 book ai didi

c# - Azure 辅助角色和 Blob 存储 C# - Microsoft.WindowsAzure.Storage.StorageException : The remote server returned an error: (400) Bad Request

转载 作者:行者123 更新时间:2023-12-03 03:09:55 24 4
gpt4 key购买 nike

我已将文件上传到 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() 在 c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon 中的 Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand1 cmd、IRetryPolicy 策略、OperationContext operationContext)\Core\Executor\Executor.cs:第 677 行
--- 内部异常堆栈跟踪结束 ---
在 c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\中的 Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand
1 cmd、IRetryPolicy 策略、OperationContext operationContext) ClassLibraryCommon\Core\Executor\Executor.cs:第 604 行 在 c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlobContainer.cs 中的 Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.CreateIfNotExists(BlobContainerPublicAccessType accessType、BlobRequestOptions requestOptions、OperationContext operationContext):第 199 行 在 WorkerRoleWithSBQueue1.WorkerRole.b__4_0(BrokeredMessage returnedMessage)

任何帮助将不胜感激。

最佳答案

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

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/en-us/library/azure/dd135715.aspx .

关于c# - Azure 辅助角色和 Blob 存储 C# - Microsoft.WindowsAzure.Storage.StorageException : The remote server returned an error: (400) Bad Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39983010/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com