2017-05-25 56 views
0

我想創建一個Azure函數,執行PowerShell與存儲隊列觸發器。出於測試目的,我希望此功能可以處理OneDrive for Business帳戶中的文件。要將文件複製到aapdftoimage/ThreePages.pdfaapdftoimage/output_ThreePages.pdfAzure函數與PowerShell,存儲隊列觸發器和OneDrive業務

當OneDrive for Business作爲輸入集成時,任何時候該函數由隊列中的新消息觸發時都會收到錯誤。如果我斷開OneDrive作爲輸入,我不會收到任何錯誤,並且包含該消息。

的錯誤是:

2017-05-25T22:24:38.484 Function started (Id=a0c37fdf-ed3c-473c-9c79-236d63531e7e) 
2017-05-25T22:24:38.499 Function completed (Failure, Id=a0c37fdf-ed3c-473c-9c79-236d63531e7e, Duration=1ms) 
2017-05-25T22:24:38.562 Exception while executing function: Functions.QueueTriggerPowerShell1. Microsoft.Azure.WebJobs.Host: No value for named parameter 'file'. 

這裏是我的PowerShell:

$inData = Get-Content $triggerInput 
$inFile = Get-Content $inputFile 

Write-Output "PowerShell script processed queue message '$inData'" 
Write-Output "inFile: $inFile" 

這裏的function.json:

{ 
    "bindings": [ 
    { 
     "name": "triggerInput", 
     "type": "queueTrigger", 
     "direction": "in", 
     "queueName": "samples-powershell-pdftoimage", 
     "connection": "<storageaccount>_STORAGE" 
    }, 
    { 
     "type": "apiHubFile", 
     "name": "inputFile", 
     "path": "aapdftoimage/{file}", 
     "connection": "onedriveforbusiness1_ONEDRIVEFORBUSINESS", 
     "direction": "in" 
    } 
    ], 
    "disabled": false 
} 

正如我寫這篇文章,我想我的一部分混淆在OneDrive for Business的輸入和輸出(未連接在我的測試中)。

我知道$triggerInput是什麼。這是消息的內容。但$inputFile是什麼? {file}從哪裏來?

我想,也許我會做以下,但它也不能正常工作(同樣的錯誤):

$file = Get-Content $triggerInput 

我想這可能定義爲$inputFile「aapdftoimage/$文件」,但它什麼都不做的分類。

不用說,我處於停滯狀態。任何人都可以給我一些指導,並理順我嗎?

回答

2

@Henry哈米德薩菲是正確的。使用C#,您可以利用Binder對象來動態命名文件。

在您的用例中,動態提供文件名的唯一方法是將其作爲JSON對象傳遞給觸發器有效內容。這是一個爲我工作的示例設置。

function.json:

{ 
    "bindings": [ 
    { 
     "name": "triggerInput", 
     "type": "queueTrigger", 
     "direction": "in", 
     "queueName": "samples-powershell", 
     "connection": "AzureWebJobsStorage" 
    }, 
    { 
     "type": "apiHubFile", 
     "name": "inputFile", 
     "path": "aapdftoimage/{file}", 
     "connection": "onedriveforbusiness_ONEDRIVEFORBUSINESS", 
     "direction": "in" 
    }, 
    { 
     "type": "apiHubFile", 
     "name": "outputFile", 
     "path": "aapdftoimage/output_{file}", 
     "connection": "onedriveforbusiness_ONEDRIVEFORBUSINESS", 
     "direction": "out" 
    } 
    ], 
    "disabled": false 
} 

運行。PS1:

$in = Get-Content $triggerInput 
Write-Output "PowerShell script processed queue message '$in'" 
Copy-Item $inputFile $outputFile 

請求體(如果使用在Portal測試窗格)或隊列觸發有效載荷:

{ 
    "file":"ThreePages.pdf" 
} 

日誌條目:

2017-05-26T22:27:53.984 Function started (Id=032c4469-8378-44ce-af9e-5a941afb0d82) 
2017-05-26T22:27:54.875 PowerShell script processed queue message '{ "file":"ThreePages.pdf" }' 
2017-05-26T22:27:54.891 Function completed (Success, Id=032c4469-8378-44ce-af9e-5a941afb0d82, Duration=899ms) 

OneDrive文件夾屏幕拍攝: enter image description here

+0

謝謝!它正在工作!我認爲這是很多魔術。很多事情都是自動發生的。我寧願明確地獲取內容,從JSON轉換,分配給{file},然後知道inputFile和outputFile是什麼。 – Chris76786777

+0

是的,你的觀察是正確的。無法在代碼中分配{file}是現階段已知的限制。這適用於處於試驗階段的Azure函數支持的所有語言。我們計劃在未來的版本中解決這個問題,但目前沒有ETA。 –

+0

太好了。聽到那個消息很開心。 – Chris76786777

1

工作實例

Function.json:

{ 
    "bindings": [ 
    { 
     "name": "triggerInput", 
     "type": "queueTrigger", 
     "direction": "in", 
     "queueName": "test", 
     "connection": "AzureWebJobsDashboard" 
    }, 
    { 
     "type": "apiHubFile", 
     "name": "inputFile", 
     "path": "aapdftoimage/ThreePages.pdf", 
     "connection": "onedrive_ONEDRIVE", 
     "direction": "in" 
    }, 
    { 
     "type": "apiHubFile", 
     "name": "outputFile", 
     "path": "aapdftoimage/output_ThreePages.pdf", 
     "connection": "onedrive_ONEDRIVE", 
     "direction": "out" 
    } 
    ], 
    "disabled": false 
} 

run.ps1:

$in = Get-Content $triggerInput 
Write-Output "PowerShell script processed queue message '$in'" 

Copy-Item $inputFile $outputFile 
+0

感謝您的回覆。在function.json中,你已經指定了要移動的文件。這是不是不可能動態運行? – Chris76786777

+0

下面是使用C#做類似的例子:https://stackoverflow.com/questions/43348985/dynamic-output-file-name-of-apihubfile-azure-function-binding-one-drive-drop-b –

相關問題