gpt4 book ai didi

Azure APIM 分段上传

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

我正在尝试通过 APIM 将文件批量上传到 Azure 存储帐户。在本地,我可以使用 REST API 将文件上传到 azure 存储帐户。但是,通过 APIM 我无法上传文件。我收到 401 错误。您能告诉我应该使用哪种 APIM 策略来通过 APIM 上传文件吗?

最佳答案

感谢@Prashant Kumar用于执行 Azure 存储文件共享操作的博客 APIM 策略。

以下是他的文档中遵循的步骤,

  • 创建了 API 管理服务和 Azure 存储帐户。
  • 然后出于示例目的,从“所有 API”中选择了 Echo API,然后从“所有操作”中的“设计”选项卡中选择“创建资源”后,并在此处获得 200 ok 响应。
  • 然后如下图所示,通过单击“编辑策略”图标并单击“保存”,在入站处理策略中添加以下策略。下面是在 和 之间使用的示例发送请求策略,如图所示。
 <!-- Initialize context variables with property values. -->

<set-variable name="storageAccount" value="NameOfYourStorageAccount" />

<set-variable name="x-request-body" value="@(context.Request.Body.As<string>())" />

<set-variable name="x-request-body-length" value="@{

return (string)context.Request.Headers.GetValueOrDefault("Content-Length","0");

}" />

<send-request mode="new" response-variable-name="tokenstate" timeout="2" ignore-error="true">

<set-url>@{

return string.Format("https://{0}.file.core.windows.net/NamOfYourFileShares/FileNamewithextension?SASToken ",(string)context.Variables["storageAccount"]);

}</set-url>

<set-method>PUT</set-method>

<set-header name="x-ms-type" exists-action="override">

<value>file</value>

</set-header>

<set-header name="x-ms-file-permission" exists-action="override">

<value>inherit</value>

</set-header>

<set-header name="x-ms-file-attributes" exists-action="override">

<value>none</value>

</set-header>

<set-header name="x-ms-file-creation-time" exists-action="override">

<value>now</value>

</set-header>

<set-header name="x-ms-file-last-write-time" exists-action="override">

<value>now</value>

</set-header>

<set-header name="x-ms-content-length" exists-action="override">

<value>65336</value>

</set-header>

</send-request>

在上面的发送请求中更改您的存储帐户、容器/文件名和 SAS token 详细信息。 enter image description here

  • 然后转到同一策略的“测试”选项卡并选择更新的资源,如下图所示。 enter image description here
  • 您将获得 200 ok 响应状态,如图所示。 enter image description here
  • 能够检索 Azure 存储中的文件。如果要检索 Blob 存储中的文件,请在上述策略中根据要求更改存储详细信息。 enter image description here

but from the APIM I am unable to upload the file as it saying 401error

注意:使用 APIM 将文件上传到 Azure 存储帐户时,会返回 401 错误,表示不允许访问该存储帐户。这可能是因为作为身份验证凭据的 SAS token 或存储帐户 key 丢失或错误。

  • 您应该验证请求中提供的身份验证凭据是否准确,并且拥有存储帐户的必要访问权限,才能解决此问题。要对请求进行身份验证和授权,您可以为存储帐户中的特定容器或 blob 创建 SAS token ,并将其包含在请求 header 中。

  • 您也可以引用这些MS documentSO问题。

关于Azure APIM 分段上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76111065/

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