ファイルをダウンロードする方法はありますか?



Qmonus SDKがマウントしているディスク上に存在するファイルをダウンロードする、あるいは別のサイトのファイルをQmonus SDKを通してプロキシダウンロードすることができます。
いづれのケースもAPIGWのroutingプラグインのカスタムスクリプトを記述する必要があります。

マウントディスク上に存在するファイルのダウンロード

APIGWのディスクに存在するファイルをストリーミングでダウンロードするroutingのforbidden_process記述例です。本APIは、APIGWで折り返すため、targetauthoritiesは無効ですので適当なダミー設定をしています。ここでは、テストを簡略化するため、scopepublicにして認可を無効化している点に注意してください。

- domain: example
  scope: public
  authorities:
    - 0.0.0.0
  proxy:
    scheme: 'http:'
    path: '/download/{filename}'
  target:
    scheme: 'http:'
    path: '/download/{filename}'
  request_forbidden_process: |-
    import aiofiles
    import os
    if not resources.get("filename", None):
        raise HTTPError(400, reason="Unspecified filename")

    session.set_header("Content-Type", "application/octet-stream")

    """Qmonus SDKの起動ディレクトリ配下にあるfilesディレクトリにダウンロード対象のファイルが存在する例
    """
    filepath = os.path.join(os.getcwd(), "files", resources["filename"])
    if not os.path.exists(filepath):
        raise HTTPError(404, reason="File Not found %r" % resources["filename"])

    async with aiofiles.open(filepath,"rb") as f:
        while True:
            data = await f.read(1024)
            if not data:
                break
            session.write(data)
            session.flush()
        session.finish()
  connect_timeout: 60
  request_timeout: 60


上記のroutingプラグインを登録したら以下のようにcurlコマンドなどで動作を確認できます。{endpoint}はお使いの環境に合わせて指定してください。

hash@UG30-hash-dev axis-stable % curl -i http://localhost:9099/download/dummy.bin --output test.bin
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 2048M    0 2048M    0     0  4394k      0 --:--:--  0:07:57 --:--:-- 7007k


ファイルダウンロードプロキシ

異なるサイトのファイルをストリーミングでダウンロードするroutingのforbidden_process記述例です。本APIは、APIGWで折り返すため、targetauthoritiesは無効ですので適当なダミー設定をしています。ここでは、テストを簡略化するため、scopepublicにして認可を無効化している点に注意してください。

- domain: example
  proxy:
    path: /downloadProxy
    scheme: 'http:'
  scope: public
  target:
    path: /downloadProxy
  authorities:
    - 0.0.0.0
  request_forbidden_process: |-
    if not parameters.get("url", []):
        raise HTTPError(400, reason="Unspecified url")

    session.set_header("Content-Type", "application/octet-stream")

    """クエリパラメータで指定された任意のURLからファイルをダウンロードする例
    """
    url = parameters["url"][0]

    def streaming_callback(chunk):
        session.write(chunk)
        session.flush()

    """ファイルダウンロード時間を考慮してタイムアウトを適切に設定する必要があります
    """
    await callout(url=url, streaming_callback=streaming_callback, request_timeout=300)
    session.finish()
  connect_timeout: 60
  request_timeout: 60


上記のroutingプラグインを登録したら以下のようにcurlコマンドなどで動作を確認できます。{endpoint}はお使いの環境に合わせて指定してください。

hash@UG30-hash-dev axis-stable % curl -i "http://localhost:9099/downloadProxy?url=http://ipv4.download.thinkbroadband.com/1GB.zip" --output test.bin
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1024M    0 1024M    0     0  6170k      0 --:--:--  0:02:49 --:--:-- 7792k