使用 Azure Function 傳回值
本文說明傳回值如何在函式內運作。 在具有傳回值的語言中,您可以將函 式輸出系結 系結至傳回值。
將 name
function.json 屬性設定為 $return
。 如果有多個輸出繫結,請只對其中一個使用傳回值。
使用傳回值的方式取決於您在函式應用程式中使用的 C# 模式:
如需詳細數據和範例,請參閱 .NET 背景工作角色指南 中的輸出系結。
以下是function.json檔案中的輸出系結:
{
"name": "$return",
"type": "blob",
"direction": "out",
"path": "output-container/{id}"
}
以下是 JavaScript 程式碼:
module.exports = function (context, input) {
var json = JSON.stringify(input);
context.log('Node.js script processed queue message', json);
return json;
}
以下是function.json檔案中的輸出系結:
{
"name": "Response",
"type": "blob",
"direction": "out",
"path": "output-container/{blobname}"
}
以下是使用 HTTP 輸出系結傳回值的 PowerShell 程式代碼:
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = $blobname
})
以下是function.json檔案中的輸出系結:
{
"name": "$return",
"type": "blob",
"direction": "out",
"path": "output-container/{id}"
}
以下是 Python 程式碼:
def main(input: azure.functions.InputStream) -> str:
return json.dumps({
'name': input.name,
'length': input.length,
'content': input.read().decode('utf-8')
})
將輸出系結註釋套用至函式方法。 如果有多個輸出繫結,請只對其中一個使用傳回值。
以下是使用輸出系結傳回值的 Java 程式代碼:
@FunctionName("QueueTrigger")
@StorageAccount("AzureWebJobsStorage")
@BlobOutput(name = "output", path = "output-container/{id}")
public static String run(
@QueueTrigger(name = "input", queueName = "inputqueue") WorkItem input,
final ExecutionContext context
) {
String json = String.format("{ \"id\": \"%s\" }", input.id);
context.getLogger().info("Java processed queue message. Item=" + json);
return json;
}
下一步
意見反映
https://aka.ms/ContentUserFeedback。
即將推出:我們會在 2024 年淘汰 GitHub 問題,並以全新的意見反應系統取代並作為內容意見反應的渠道。 如需更多資訊,請參閱:提交及檢視以下的意見反映: