【发布时间】:2019-01-16 13:18:20
【问题描述】:
我有一个 Azure Function v1、SDK 1.0.24 试图访问我的存储队列,它在本地运行良好,我可以看到正确存储的消息。但是一旦我将它发布到云端,它就会失败并显示 403 禁止,并且我已经没有线索了。
我检查了几次连接字符串,我检查了请求和响应中的时间戳,这些都很好。我尝试更新了几个NuGet包,但最后为什么它们坏了应该在本地工作而不是在线?我没有使用 Application Insights。在主机日志中我发现了这个错误:
2019-01-16T12:38:32.460 [详细] 主机 '44bf8a95b6652eed85464155b2b48df2' 未能获取主机锁租约: Microsoft.WindowsAzure.Storage:远程服务器返回错误: (403) 禁止。
我怀疑 Azure 中存在阻止访问的安全相关设置(但我对安全功能没有任何控制权,管理员也不知道可能是什么阻止问题)。
问题发生在 QueueTrigger 上,因此我创建了一个具有替代访问权限的小函数来重现该问题:
public static class TestStorageQueue
{
[FunctionName("TestStorageQueue")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req,
TraceWriter log)
{
log.Info("START");
try
{
var response = new HttpResponseMessage(HttpStatusCode.OK);
log.Info(ConfigurationManager.ConnectionStrings["soastorage"]?.ConnectionString);
CloudStorageAccount storeAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["soastorage"]?.ConnectionString);
CloudQueueClient queueClient = storeAccount.CreateCloudQueueClient();
CloudQueue queue = queueClient.GetQueueReference("myqueue");
log.Info("trying to get message from queue");
var cloudMessage = queue.GetMessage(); // 403 happens here
log.Info("received message item");
var message = cloudMessage?.AsBytes;
var length = message?.Length ?? 0;
response.Content = new StringContent("received message length: " + length.ToString());
return response;
}
catch(Exception ex)
{
var response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
response.Content = new StringContent(ex.Message);
return response;
}
}
}
更新 好笑,找了2天的答案,一贴出来,就找到了原因。问题在于 Azure 存储防火墙,即使将所有 MS 服务列入白名单,它也会一直阻止它们。所以临时的解决办法是把它关掉,这不是真正的解决办法,所以问题仍然悬而未决
【问题讨论】:
-
在完全相同的情况下使用 v2 功能,同样的问题对我不起作用。这很尴尬。
标签: c# .net azure azure-functions azure-storage-queues