如何在API Management的ARM模板中获取Blob内的GraphQL Schema内容
从Blob加载GraphQL Schema到APIM的ARM模板解决方案
问题说明
我的GraphQL Schema内容体积较大,希望将其存储在Blob容器中,并通过API Management的ARM模板获取该内容。已在同一区域存储了链接模板,且能生成带SAS Token的URI,但目前无法获取Blob中的内容并加载到字符串变量里——当前代码仅把URI本身转成了base64,需要可行的Blob内容获取方法。
解决方案
ARM模板本身不支持直接读取外部Blob内容,需通过**部署脚本(Deployment Script)**实现:部署脚本会调用Azure存储相关命令读取Blob内容,返回后供APIM的Schema资源使用。
修改后的ARM模板
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "ApimServiceName": { "type": "string" }, "appInsightsName": { "type": "string" }, "_artifactsLocation": { "type": "string" }, "_artifactsLocationSasToken": { "type": "string" }, "_buildNumber": { "type": "string" }, "KeyvaultName": { "type": "string" } }, "variables": { "schemaUri": "[concat(parameters('_artifactsLocation'), parameters('_buildNumber'), '/sample-graphql-schema.graphql', parameters('_artifactsLocationSasToken'))]", "deploymentScriptName": "fetchGraphQLSchema" }, "resources": [ { "apiVersion": "2021-08-01", "type": "Microsoft.ApiManagement/service/apis", "name": "[concat(parameters('ApimServiceName'), '/graphql-sample')]", "properties": { "description": "", "authenticationSettings": { "subscriptionKeyRequired": false }, "subscriptionKeyParameterNames": { "header": "Ocp-Apim-Subscription-Key", "query": "subscription-key" }, "type": "graphql", "apiRevision": "1", "subscriptionRequired": true, "displayName": "GraphQL API", "serviceUrl": "https://emea.sample.com/api/graphql", "path": "graphql-sample", "protocols": [ "http", "https" ] } }, { "type": "Microsoft.Resources/deploymentScripts", "apiVersion": "2020-10-01", "name": "[variables('deploymentScriptName')]", "location": "[resourceGroup().location]", "kind": "AzurePowerShell", "properties": { "azPowerShellVersion": "7.2", "scriptContent": " $schemaUri = '${schemaUri}' $schemaContent = Invoke-RestMethod -Uri $schemaUri -Method Get return @{ schemaContent = $schemaContent } ", "retentionInterval": "PT1H", "cleanupPreference": "OnSuccess" } }, { "apiVersion": "2021-08-01", "type": "Microsoft.ApiManagement/service/apis/schemas", "name": "[concat(parameters('ApimServiceName'), '/graphql-sample/graphql')]", "dependsOn": [ "[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), 'graphql-sample')]", "[resourceId('Microsoft.Resources/deploymentScripts', variables('deploymentScriptName'))]" ], "properties": { "contentType": "application/vnd.ms-azure-apim.graphql.schema", "document": { "value": "[concat('data:text/plain;charset=utf-8;base64,', base64(reference(resourceId('Microsoft.Resources/deploymentScripts', variables('deploymentScriptName'))).outputs.schemaContent))]" } } } ] }
关键修改点
- 新增部署脚本资源:用PowerShell脚本调用
Invoke-RestMethod读取Blob中的Schema内容,执行完成后将内容作为输出返回 - 配置依赖关系:让APIM的Schema资源依赖于部署脚本,确保脚本执行完成、内容获取后再创建Schema
- 引用脚本输出内容:通过
reference()函数获取部署脚本的输出结果,转成base64格式后填入document.value字段
注意事项
- 确保SAS Token包含Blob的读取权限,否则脚本无法获取内容
- 可根据环境调整
azPowerShellVersion参数,匹配可用的PowerShell版本 - 按需修改
retentionInterval和cleanupPreference,控制部署脚本资源的保留时间
内容的提问来源于stack exchange,提问作者drewglew
相关产品推荐
相关产品推荐

