ハウツーガイド - CLIでのアップロードストリーミングアクション/トリガーの実行
このセグメントでは、Workato Gemを使用して、ファイルストリーミングを利用するアクションを実行し、簡単にデバッグする方法について説明します。 ファイルストリーミングは、連携して機能する2つの主要コンポーネント、ダウンロードファイルアクションとアップロードファイルアクションに分けられます。 以下では、CLIでアップロードファイルアクションを実行する手順について説明します。
前提条件
- Workato SDK Gemをインストール済みで、実行できること。 詳細については、はじめにガイドを参照してください。
- SDKでのファイルストリーミングの仕組みを理解している。 詳細については、ガイドをお読みください。
ファイルのアップロード - サンプルコネクター - URLへのファイルのアップロード
例として、汎用のURLへのファイルアップロードコネクターを使用します。
アップロードファイルアクションのexecute lambdaの実行
アップロードファイルストリーミングアクションでは、アクションが利用する受信ファイルストリームに関する情報を含める必要があるため、ここでは入力の定義が重要です。 ファイルストリームをシミュレートするための多数のオプションについて説明します。
execute: lambda do |_connection, input, _input_schema, _output_schema, closure|
# Calling workato.stream.in runs in a loop where the input should be file.
# It can accept both entire files or the output of a streaming-enabled download file action
workato.stream.in(input["file"]) do |chunk, starting_byte_range, ending_byte_range, eof, next_starting_byte_range|
put(input['url']).
headers("Content-Range": "bytes #{starting_byte_range}-#{ending_byte_range}/*").
request_body(chunk).presence
end
# This commits the upload
post(input['url'], { "commit": true } )
end,execute lambdaに加えて、SDK CLIでアップロードファイルアクションを実行する際には、upload_file_input.jsonなどの入力JSONファイルも必要です。 以下に、チャンクが明示的に定義されたモックストリームの例を示します。各チャンクはchunksハッシュ内の個別のキーです。
{
"file_name": "sample_file",
"file": {
# this hash simulates a file stream which is
# the output of a download file object
"__stream__": true,
"chunks": {
"0": "abcd",
"4": "efgh",
"8": "ijkl",
"12": "mn"
}
},
"url": "https://www.friendly_upload_url.com"
}アップロードファイルアクションを実行するには、標準アクションの場合と同じコマンドを指定します。
workato exec actions.upload_to_url.execute --input='upload_file_input.json' --verbose
SETTINGS
{}
INPUT
{
"file_name": "sample_file",
"file": {
"__stream__": true,
"chunks": {
"0": "abcd",
"4": "efgh",
"8": "ijkl",
"12": "mn"
}
},
"url": "https://www.friendly_upload_url.com"
}
RestClient.put "https://www.friendly_upload_url.com", "Content-Range"=>"bytes=0-3", "User-Agent"=>"rest-client/2.0.2 (darwin19.6.0 x86_64) ruby/2.4.10p364"
# => 201 Created | 0 bytes, 1.46s
RestClient.put "https://www.friendly_upload_url.com", "Content-Range"=>"bytes=4-7", "User-Agent"=>"rest-client/2.0.2 (darwin19.6.0 x86_64) ruby/2.4.10p364"
# => 201 Created | 0 bytes, 1.46s
RestClient.put "https://www.friendly_upload_url.com", "Content-Range"=>"bytes=8-11", "User-Agent"=>"rest-client/2.0.2 (darwin19.6.0 x86_64) ruby/2.4.10p364"
# => 201 Created | 0 bytes, 1.46s
RestClient.put "https://www.friendly_upload_url.com", "Content-Range"=>"bytes=11-13", "User-Agent"=>"rest-client/2.0.2 (darwin19.6.0 x86_64) ruby/2.4.10p364"
# => 201 Created | 0 bytes, 1.46s
RestClient.post "https://www.friendly_upload_url.com", "{\"commit\":true}", "Accept"=>"application/json", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"88", "Content-Type"=>"application/json", "User-Agent"=>"rest-client/2.0.2 (darwin19.6.0 x86_64) ruby/2.4.10p364"
# => 201 Created | 0 bytes, 1.46s
OUTPUT
{
"file_size": 13,
"file_path": "/path/to/sample/file",
"file_name": "file_name"
}ストリームをモックするバリエーション
チャンクを手動で宣言してストリームをモックする以外にも、さまざまな方法でストリームをモックできます。
TIP
ファイルのサイズやネットワークによっては、ストリームの実行完了に平均的なアクションよりも時間がかかる場合があることに注意してください。 プロダクションサイズのファイルではなく、より小さなファイルでテストすることをお勧めします。
各チャンクを明示的に指定してストリームをモックする
{
"file": {
"__stream__": true,
"chunks": {
"0": "abcd",
"4": "efgh",
"8": "ijkl",
"12": "mn"
}
}
}同じコネクター内のダウンロードファイルアクション/トリガー用に実装されたストリームを利用してストリームをモックする
{
"file": {
"__stream__": true,
"name": "stream_within_same_connector", # name of the stream in the connector
"input": { # input that will be passed to the stream callback
"file_path": "/path/to/sample/file"
}
},
}静的ストリームを指定してストリームをモックする
{
"file": {
"data": "123456789",
"eof": true
},
}文字列を指定してストリームをモックする
{
"file": "qwertyuiop[]"
}最終更新日: