Azure blob 返回 403 禁止在门户上运行的 Azure 函数

标签 azure azure-functions azure-blob-storage

我读过几篇有关类似查询的帖子,例如 this one ,但我不断收到 403。

最初,我在 Visual Studio 中编写代码 - 访问存储 blob 的 azure 函数 - 一切运行正常。但是当我部署同样的函数时,它抛出 403!我尝试了建议的方法,移至 x64 等并删除其他文件,但没有任何效果。

请注意 - 我已验证多次 - 访问 key 正确且有效

所以,我做了以下所有事情

(1) - 我在 Portal 本身上编写了一个简单的 Azure 函数(以排除部署怪癖),瞧,同样的 403!

var storageConnection = "DefaultEndpointsProtocol=https;AccountName=[name];AccountKey=[key1];EndpointSuffix=core.windows.net";
var cloudStorageAccount = CloudStorageAccount.Parse(storageConnection);
var blobClient = cloudStorageAccount.CreateCloudBlobClient();

var sourceContainer = blobClient.GetContainerReference("landing");
CloudBlockBlob blob = container.GetBlockBlobReference("a.xlsx");

using (var inputStream = new MemoryStream())
{
    log.Info($"Current DateTime: {DateTime.Now}");
    log.Info("Starting download of blob...");
    blob.DownloadToStream(inputStream); // <--- 403 thrown here!!
    log.Info("Download Complete!");
}

(2) - 我通过记录日期时间及其函数服务器上的 UTC 来验证它

(3) - 我使用了在门户上生成的帐户 SAS key ,但仍然给出 403。在 SAS key 生成后,我等待了 30 多秒,以确保 SAS key 传播。

var sasUri = "https://[storageAccount].blob.core.windows.net/?sv=2017-11-09&ss=b&srt=sco&sp=rwdlac&se=2019-07-31T13:08:46Z&st=2018-09-01T03:08:46Z&spr=https&sig=Hm6pA7bNEe8zjqVelis2y842rY%2BGZg5CV4KLn288rCg%3D";
StorageCredentials accountSAS = new StorageCredentials(sasUri);
var cloudStorageAccount = new CloudStorageAccount(accountSAS, "[storageAccount]", endpointSuffix: null, useHttps: true);

// rest of the code same as (1)

(4) - 我在代码中动态生成了 SAS key ,但还是 403。

static string GetContainerSasUri(CloudBlobContainer container)
{
    //Set the expiry time and permissions for the container.
    //In this case no start time is specified, so the shared access signature becomes valid immediately.
    SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
    sasConstraints.SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5);
    sasConstraints.SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(25);
    sasConstraints.Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Add | SharedAccessBlobPermissions.Create;

    //Generate the shared access signature on the container, setting the constraints directly on the signature.
    string sasContainerToken = container.GetSharedAccessSignature(sasConstraints);

    //Return the URI string for the container, including the SAS token.
    return container.Uri + sasContainerToken + "&comp=list&restype=container";
}

并将以上内容用作

var sourceContainer = blobClient.GetContainerReference("landing");
var sasKey = GetContainerSasUri(sourceContainer);
var container = new CloudBlobContainer(new Uri(sasKey));

CloudBlockBlob blob = container.GetBlockBlobReference("a.xlsx"); 

我完全无法理解为什么代码在从 Visual Studio 运行、访问云上的存储(而不是模拟器)时可以完美地工作,但是当在门户上部署或显式运行相同的代码时,它无法运行。

我在这里缺少什么?

最佳答案

由于您已经排除了许多可能的原因,因此我重现您的问题的唯一方法是在存储帐户上配置防火墙。

在本地,代码可以正常工作,因为您可能已将本地 IP 添加到白名单中,而功能则省略了此步骤。在门户上,转到平台功能下的资源浏览器。搜索 outboundIpAddresses 并将这些(通常是四个)IP 添加到存储帐户白名单中。

如果您已添加功能 IP 但仍收到 403 错误,请检查存储和功能应用程序的位置。如果它们生活在同一地区(例如都在美国中部),则两者以内部方式进行通信,而无需通过 outboundIpAddresses。如果您的计划中需要防火墙,我可以提供的解决方法是在不同区域创建存储。否则就允许所有网络进行存储。

关于Azure blob 返回 403 禁止在门户上运行的 Azure 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52125430/

相关文章:

azure - 使用 Azure Windows VM 运行 PowerShell 脚本的自定义数据

azure - @azure/identitycredentials.getToken ('openid' )在配置了环境变量的情况下返回 null 而不是 DefaultAzureCredential() 的 token ?

Azure Application Insights 警报、自定义事件和 Docker

azure - 在 Azure Function App 中查找高 CPU 的特定函数

azure - Windows Server 2012 Azure VM通过https间歇性访问

Azure 事件网格多次触发 Azure Function App,而不是一次

azure - 是否可以使用 1 个输出绑定(bind)在 cosmosDB 中创建 2 个或更多文档

azure - Azure blob 名称的最大长度是多少

c# - 从 Azure Blob 存储读取数据时 OpenReadAsync() 出现奇怪的结果

azure - Windows Azure - 自动备份什么?我需要手动备份什么?