当前位置: 首页 > 知识库问答 >
问题:

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

笪俊迈
2023-03-14

我读过几篇关于类似查询的帖子,比如这篇,但我一直收到403条。

最初我在Visual Studio中编写了代码——访问存储blob的azure函数——并且一切运行良好。但是当我部署相同的函数时,它会抛出403!我尝试了建议的方法,移动到x64等并删除了其他文件,但都不起作用。

请注意-我已经验证了几次-访问密钥是正确和有效的。

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

(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 密钥,但仍提供 403。在 SAS 密钥生成后,我等待了 30 多秒,以确保 SAS 密钥能够传播。

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密钥,但还是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"); 

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

我错过了什么?

共有1个答案

长孙谦
2023-03-14

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

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

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

 类似资料:
  • 我正在调用CloudBlobContainer。CreateIfNotExist(请参阅下面的FindOrCreatePrivate ateBlobContainer方法)间接来自Web API服务,但它返回以下403禁止错误消息: 以下是生成错误的代码: 我需要一些帮助来解决此错误的原因。我尝试了以下方法: 已检查要创建的容器的名称是否有效,并且在这种特定情况下,仅由小写字母组成(无特殊或大写字

  • 问题内容: 我正在尝试制作Sitecraper。我是在本地计算机上制作的,在那儿工作得很好。当我在服务器上执行相同操作时,它显示403禁止错误。我正在使用PHP简单HTML DOM解析器 。我在服务器上收到的错误是这样的: 警告:file_get_contents(http://example.com/viewProperty.html?id=7715888)[function.file- get

  • 我使用http://localhost:8080/auth/realms/{realm_name}/protocol/openid-connect/token endpoint创建令牌。 grant_type=client_credentials 客户端-id:------------- 客户端-secret:78296D38-CC82-4010-A817-65C283484E51 现在我想获得r

  • 我有一个Azure函数,应该处理存储帐户容器中的文件。应每天触发Azure函数。但是每周一两次,我看到容器中的一些文件没有被处理:(开始时,600中的401个文件没有被处理,因为错误403) 正如我在Stackoverflow中读到的。这是因为SAS令牌时间偏差。有没有可能在我的代码中没有大的变化,我解决了这个问题? 我的代码: 最新消息 执行函数时出现消息异常:通过\u http\u触发器\u活

  • 对localhost的Ajax请求返回403错误。然而,当我更改controller requestMethod以获取并在浏览器上打开请求“url”时,它会显示从服务器返回的json数据。我希望使用这些数据填充一个下拉列表。请帮帮我。我正在使用spring security 4.0和spring MVC框架。CSRF未禁用。此外,该url在Spring得到保护。 我在这里查看了相关问题,但没有找到

  • 问题内容: 我编写了一个测试UsersController的单元测试。UsersControllerTest.findUser工作正常,但不能正常运行UsersControllerTest.insertGetModifyDelete。 在测试日志中,我可以看到POST请求与UsersController的任何方法都不匹配,但是我不明白为什么。您能帮我这个吗? 这是我其余的Java类: 我有2种方法